-
Notifications
You must be signed in to change notification settings - Fork 231
/
Copy pathCALayer+Stellar.swift
executable file
·264 lines (217 loc) · 8.69 KB
/
CALayer+Stellar.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
//Copyright (c) 2016
//
//Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
//The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import UIKit
extension CALayer: BasicConfigurable, SnapConfigurable, AttachmentConfigurable, GravityConfigurable, StepControllable {
//MARK: animation methods
public func moveX(_ increment: CGFloat) -> Self {
let type = AnimationType(type: .basic, subType: .moveX(increment))
context.addAnimationType(type)
return self
}
public func moveY(_ increment: CGFloat) -> Self {
let type = AnimationType(type: .basic, subType: .moveY(increment))
context.addAnimationType(type)
return self
}
public func moveTo(_ point: CGPoint) -> Self {
let type = AnimationType(type: .basic, subType: .moveTo(point))
context.addAnimationType(type)
return self
}
public func makeColor(_ color: UIColor) -> Self {
let type = AnimationType(type: .basic, subType: .color(color))
context.addAnimationType(type)
return self
}
public func makeOpacity(_ opacity: Float) -> Self {
let type = AnimationType(type: .basic, subType: .opacity(opacity))
context.addAnimationType(type)
return self
}
public func makeAlpha(_ alpha: CGFloat) -> Self {
let type = AnimationType(type: .basic, subType: .opacity(Float(alpha)))
context.addAnimationType(type)
return self
}
public func rotate(_ z: CGFloat) -> Self {
let type = AnimationType(type: .basic, subType: .rotate(z))
context.addAnimationType(type)
return self
}
public func rotateX(_ x: CGFloat) -> Self {
let type = AnimationType(type: .basic, subType: .rotateX(x))
context.addAnimationType(type)
return self
}
public func rotateY(_ y: CGFloat) -> Self {
let type = AnimationType(type: .basic, subType: .rotateY(y))
context.addAnimationType(type)
return self
}
public func rotateXY(_ xy: CGFloat) -> Self {
let type = AnimationType(type: .basic, subType: .rotateXY(xy))
context.addAnimationType(type)
return self
}
public func makeWidth(_ width: CGFloat) -> Self {
let type = AnimationType(type: .basic, subType: .width(width))
context.addAnimationType(type)
return self
}
public func makeHeight(_ height: CGFloat) -> Self {
let type = AnimationType(type: .basic, subType: .height(height))
context.addAnimationType(type)
return self
}
public func makeSize(_ size: CGSize) -> Self {
let type = AnimationType(type: .basic, subType: .size(size))
context.addAnimationType(type)
return self
}
public func makeFrame(_ frame: CGRect) -> Self {
let type = AnimationType(type: .basic, subType: .frame(frame))
context.addAnimationType(type)
return self
}
public func makeBounds(_ bounds: CGRect) -> Self {
let type = AnimationType(type: .basic, subType: .bounds(bounds))
context.addAnimationType(type)
return self
}
public func scaleX(_ x: CGFloat) -> Self {
let type = AnimationType(type: .basic, subType: .scaleX(x))
context.addAnimationType(type)
return self
}
public func scaleY(_ y: CGFloat) -> Self {
let type = AnimationType(type: .basic, subType: .scaleY(y))
context.addAnimationType(type)
return self
}
public func scaleXY(_ x: CGFloat, _ y: CGFloat) -> Self {
let type = AnimationType(type: .basic, subType: .scaleXY(x,y))
context.addAnimationType(type)
return self
}
public func cornerRadius(_ radius: CGFloat) -> Self {
let type = AnimationType(type: .basic, subType: .cornerRadius(radius))
context.addAnimationType(type)
return self
}
public func borderWidth(_ width: CGFloat) -> Self {
let type = AnimationType(type: .basic, subType: .borderWidth(width))
context.addAnimationType(type)
return self
}
public func shadowRadius(_ radius: CGFloat) -> Self {
let type = AnimationType(type: .basic, subType: .shadowRadius(radius))
context.addAnimationType(type)
return self
}
public func zPosition(_ position: CGFloat) -> Self {
let type = AnimationType(type: .basic, subType: .zPosition(position))
context.addAnimationType(type)
return self
}
public func anchorPoint(_ point: CGPoint) -> Self {
let type = AnimationType(type: .basic, subType: .anchorPoint(point))
context.addAnimationType(type)
return self
}
public func anchorPointZ(_ z: CGFloat) -> Self {
let type = AnimationType(type: .basic, subType: .anchorPointZ(z))
context.addAnimationType(type)
return self
}
public func shadowOffset(_ offset: CGSize) -> Self {
let type = AnimationType(type: .basic, subType: .shadowOffset(offset))
context.addAnimationType(type)
return self
}
public func shadowColor(_ color: UIColor) -> Self {
let type = AnimationType(type: .basic, subType: .shadowColor(color))
context.addAnimationType(type)
return self
}
public func shadowOpacity(_ opacity: Float) -> Self {
let type = AnimationType(type: .basic, subType: .shadowOpacity(opacity))
context.addAnimationType(type)
return self
}
public func makeTintColor(_ color: UIColor) -> Self {
let type = AnimationType(type: .basic, subType: .tintColor(color))
context.addAnimationType(type)
return self
}
public func completion(_ c: @escaping () -> Void) -> Self {
context.changeCompletion(c)
return self
}
//MARK: Physical Animation
//Snap
public func snap(_ damping: CGFloat = 0.5) -> Self {
context.changeMainType(.snap(damping))
return self
}
//Attachment
public func attachment(_ damping: CGFloat = 0.5, frequency: CGFloat = 0.5) -> Self {
context.changeMainType(.attachment(damping, frequency))
return self
}
//Gravity
public func gravity(_ magnitude: Double = 1.0) -> Self {
context.changeMainType(.gravity(magnitude))
return self
}
//MARK: Basic Animation configurations
public func duration(_ d: CFTimeInterval) -> Self {
context.changeDuration(d)
return self
}
public func easing(_ type: TimingFunctionType) -> Self {
context.changeEasing(type)
return self
}
public func delay(_ d: CFTimeInterval) -> Self {
context.changeDelay(d)
return self
}
public func reverses() -> Self {
context.changeAutoreverses(true)
return self
}
public func repeatCount(_ count: Int) -> Self {
context.changeRepeatCount(count)
return self
}
//MARK: Chainable methods
public func then() -> Self {
context.makeNextStep()
return self
}
//commit to excute
public func animate() -> Void {
context.commit()
}
//MARK: StepControllable methods
public func cancelAllRemaining() {
context.removeAllRemaining()
}
//Private Context for view and layer
fileprivate var context: AnimationContext {
get {
let identifier = String(describing: Unmanaged.passUnretained(self).toOpaque())
var context = self.value(forKey: identifier) as? AnimationContext
if context == nil {
context = AnimationContext(object: self)
self.setValue(context!, forKey: identifier)
}
return context!
}
}
}