-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKeiAnimatedPath.swift
354 lines (270 loc) · 11.7 KB
/
KeiAnimatedPath.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
//
// KeiAnimatedPath.swift
//
// Created by keisyrzk on 16.02.2017.
// Copyright © 2017 keisyrzk. All rights reserved.
//
import UIKit
import QuartzCore
import CoreText
import CoreGraphics
class KeiAnimatedPath
{
////////////////////////
// CREATE A SINGLETON //
class var shared: KeiAnimatedPath
{
struct Singleton
{
static let instance = KeiAnimatedPath()
}
return Singleton.instance
}
// CREATE A SINGLETON //
////////////////////////
var animationLayer: CALayer?
var pathLayer: CAShapeLayer?
private var inputPath: CGPath!
private var inputDuration: CFTimeInterval = 10
private var inputLineWidth: CGFloat = 10
private var inputLineColor = UIColor.black
private var inputRotationAngle: CGFloat = 0
private var inputPolygonSidesNumber: Int = 6
private var inputCornerRadius: Float = 8
private var inputText = ""
private var inputFontSize: CGFloat = 20
private var inputFontName = "PingFangSC-Bold"
func drawAnimatedCustomPath(in view: UIView, path: CGPath, duration: CFTimeInterval, lineWidth: CGFloat, lineColor: UIColor)
{
self.inputDuration = duration
self.inputLineWidth = lineWidth
self.inputLineColor = lineColor
self.inputPath = path
animationLayer = CALayer()
animationLayer?.frame = CGRect(x: 0, y: 0, width: view.layer.bounds.width, height: view.layer.bounds.height)
view.layer.addSublayer(animationLayer!)
view.clipsToBounds = true
setupDrawingLayer()
startAnimation()
}
func drawAnimatedRectanglePath(in view: UIView, duration: CFTimeInterval, lineWidth: CGFloat, lineColor: UIColor)
{
self.inputDuration = duration
self.inputLineWidth = lineWidth
self.inputLineColor = lineColor
self.inputPath = rectanglePath(view: view)
animationLayer = CALayer()
animationLayer?.frame = CGRect(x: 0, y: 0, width: view.layer.bounds.width, height: view.layer.bounds.height)
view.layer.addSublayer(animationLayer!)
view.clipsToBounds = true
setupDrawingLayer()
startAnimation()
}
func drawAnimatedPolygonPath(in view: UIView, numberOfSides polygonSidesNumber: Int?, rotationAngle: CGFloat?, polygonCornerRadius: Float?, duration: CFTimeInterval, lineWidth: CGFloat, lineColor: UIColor)
{
self.inputDuration = duration
self.inputLineWidth = lineWidth
self.inputLineColor = lineColor
if let _rotationAngle = rotationAngle
{
self.inputRotationAngle = _rotationAngle
}
if let _polygonSidesNumber = polygonSidesNumber
{
self.inputPolygonSidesNumber = _polygonSidesNumber
}
if let _polygonCornerRadius = polygonCornerRadius
{
self.inputCornerRadius = _polygonCornerRadius
}
self.inputPath = polygonPath(view: view)
animationLayer = CALayer()
animationLayer?.frame = CGRect(x: 0, y: 0, width: view.layer.bounds.width, height: view.layer.bounds.height)
view.layer.addSublayer(animationLayer!)
setupDrawingLayer()
startAnimation()
}
func drawAnimatedText(in view: UIView, with text: String, duration: CFTimeInterval, lineWidth: CGFloat, textColor: UIColor, fontName: String?, fontSize: CGFloat?)
{
self.inputText = text
self.inputDuration = duration
self.inputLineWidth = lineWidth
self.inputLineColor = textColor
if let _fontSize = fontSize
{
self.inputFontSize = _fontSize
}
if let _fontName = fontName
{
self.inputFontName = _fontName
}
animationLayer = CALayer()
animationLayer?.frame = CGRect(x: 0, y: 0, width: view.layer.bounds.width, height: view.layer.bounds.height)
view.layer.addSublayer(animationLayer!)
setupTextLayer(in: view)
startAnimation()
}
func setupDrawingLayer()
{
clearLayer()
if let _ = animationLayer
{
let pathRect: CGRect = animationLayer!.bounds
let pathShapeLayer = CAShapeLayer()
pathShapeLayer.frame = animationLayer!.bounds
pathShapeLayer.bounds = pathRect
pathShapeLayer.isGeometryFlipped = true
pathShapeLayer.path = inputPath
pathShapeLayer.strokeColor = inputLineColor.cgColor
pathShapeLayer.fillColor = nil
pathShapeLayer.lineWidth = inputLineWidth
pathShapeLayer.lineJoin = kCALineJoinBevel
animationLayer!.addSublayer(pathShapeLayer)
pathLayer = pathShapeLayer
}
}
func setupTextLayer(in view: UIView)
{
clearLayer()
if let _ = animationLayer
{
let font = CTFontCreateWithName(inputFontName as CFString?, inputFontSize, nil)
let attrStr = NSAttributedString(string: inputText, attributes: [kCTFontAttributeName as String: font])
let line = CTLineCreateWithAttributedString(attrStr)
let runArray = CTLineGetGlyphRuns(line)
let letters = CGMutablePath()
for runIndex in 0..<CFArrayGetCount(runArray) {
let runUnsafe: UnsafeRawPointer = CFArrayGetValueAtIndex(runArray, runIndex)
let run = unsafeBitCast(runUnsafe, to: CTRun.self)
for runGlyphIndex in 0..<CTRunGetGlyphCount(run) {
let thisGlyphRange = CFRangeMake(runGlyphIndex, 1)
var glyph: CGGlyph = CGGlyph()
var position: CGPoint = CGPoint()
CTRunGetGlyphs(run, thisGlyphRange, &glyph)
CTRunGetPositions(run, thisGlyphRange, &position)
let letter = CTFontCreatePathForGlyph(font, glyph, nil)
let t = CGAffineTransform(translationX: position.x, y: position.y);
if letter == nil {
continue
}
letters.addPath(letter!, transform:t)
}
}
let path = UIBezierPath()
path.move(to: CGPoint.zero)
path.append(UIBezierPath(cgPath: letters))
let layer = CAShapeLayer()
layer.frame = CGRect(x: 0, y: 0, width: view.layer.bounds.width, height: view.layer.bounds.height)
layer.isGeometryFlipped = true
layer.path = path.cgPath
layer.strokeColor = inputLineColor.cgColor
layer.fillColor = nil
layer.lineWidth = inputLineWidth
layer.lineJoin = kCALineJoinBevel
animationLayer!.addSublayer(layer)
pathLayer = layer
}
}
private func startAnimation()
{
pathLayer?.removeAllAnimations()
let pathAnimation = CABasicAnimation(keyPath: "strokeEnd")
pathAnimation.duration = inputDuration
pathAnimation.fromValue = 0.0
pathAnimation.toValue = 1.0
pathLayer?.add(pathAnimation, forKey: "strokeEnd")
}
//freezes the animation until clearLayer() is called
func stopAnimatingWithPause()
{
if let pausedTime = pathLayer?.convertTime(CACurrentMediaTime(), from: nil)
{
pathLayer?.speed = 0.0
pathLayer?.timeOffset = pausedTime
}
}
//clears the animation layers
func stopAnimatingWithClear()
{
clearLayer()
}
func clearLayer()
{
if let _ = pathLayer
{
pathLayer?.removeFromSuperlayer()
pathLayer = nil
}
}
/// CREATE PATH ///
private func rectanglePath(view: UIView) -> CGPath
{
//start point - left-down corner
var point = CGPoint(x: inputLineWidth/2, y: 0)
let path = UIBezierPath()
path.move(to: point)
//left-up corner
point = CGPoint(x: inputLineWidth/2, y: view.frame.height)
path.addLine(to: point)
path.move(to: CGPoint(x: inputLineWidth/2, y: view.frame.height - inputLineWidth/2))
//right-up corner
point = CGPoint(x: view.frame.width, y: view.frame.height - inputLineWidth/2)
path.addLine(to: point)
path.move(to: CGPoint(x: view.frame.width - inputLineWidth/2, y: view.frame.height - inputLineWidth/2))
//right-down corner
point = CGPoint(x: view.frame.width - inputLineWidth/2, y: 0)
path.addLine(to: point)
path.move(to: CGPoint(x: view.frame.width - inputLineWidth/2, y: inputLineWidth/2))
//start point - left-down corner
point = CGPoint(x: 0, y: inputLineWidth/2)
path.addLine(to: point)
path.close()
return path.cgPath
}
private func polygonPath(view: UIView) -> CGPath
{
let path = UIBezierPath()
let theta = Float(2.0 * M_PI) / Float(inputPolygonSidesNumber)
let offset = inputCornerRadius * tanf(theta / 2.0)
let squareWidth = Float(min(view.frame.size.width, view.frame.size.height))
var length = squareWidth - Float(inputLineWidth)
if inputPolygonSidesNumber % 4 != 0
{
length = length * cosf(theta / 2.0) + offset / 2.0
}
let sideLength = length * tanf(theta / 2.0)
var point = CGPoint(x: CGFloat((squareWidth / 2.0) + (sideLength / 2.0) - offset), y: CGFloat(squareWidth - (squareWidth - length) / 2.0))
var angle = Float(M_PI)
path.move(to: point)
for _ in 0 ..< inputPolygonSidesNumber
{
let x = Float(point.x) + (sideLength - offset * 2.0) * cosf(angle)
let y = Float(point.y) + (sideLength - offset * 2.0) * sinf(angle)
point = CGPoint(x: CGFloat(x), y: CGFloat(y))
path.addLine(to: point)
let centerX = Float(point.x) + inputCornerRadius * cosf(angle + Float(M_PI_2))
let centerY = Float(point.y) + inputCornerRadius * sinf(angle + Float(M_PI_2))
let center = CGPoint(x: CGFloat(centerX), y: CGFloat(centerY))
let startAngle = CGFloat(angle) - CGFloat(M_PI_2)
let endAngle = CGFloat(angle) + CGFloat(theta) - CGFloat(M_PI_2)
path.addArc(withCenter: center, radius: CGFloat(inputCornerRadius), startAngle: startAngle, endAngle: endAngle, clockwise: true)
point = path.currentPoint
angle += theta
}
path.close()
// ROTATE
//get the center and transform the path so it's centered at the origin
let bounds = path.cgPath.boundingBox
let center = CGPoint(x:bounds.midX, y:bounds.midY)
let toOrigin = CGAffineTransform(translationX: -center.x, y: -center.y)
path.apply(toOrigin)
//rotate around the origin
let rotAngle = inputRotationAngle * CGFloat(M_PI) / 180
let rotation = CGAffineTransform(rotationAngle: CGFloat(rotAngle))
path.apply(rotation)
//translate back to the origin
let fromOrigin = CGAffineTransform(translationX: center.x, y: center.y)
path.apply(fromOrigin)
return path.cgPath
}
}//end of class