forked from ruanjx/VideoLab
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimingFunction.swift
More file actions
302 lines (267 loc) · 8.85 KB
/
Copy pathTimingFunction.swift
File metadata and controls
302 lines (267 loc) · 8.85 KB
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
//
// TimingFunction.swift
// VideoLab
//
// Created by Bear on 2020/8/10.
// Copyright © 2020 Chocolate. All rights reserved.
//
import CoreMedia
public enum TimingFunction: Int {
// Linear interpolation (no easing)
case linear
// Quadratic easing p^2
case quadraticEaseIn
case quadraticEaseOut
case quadraticEaseInOut
// Cubic easing p^3
case cubicEaseIn
case cubicEaseOut
case cubicEaseInOut
// Quartic easing p^4
case quarticEaseIn
case quarticEaseOut
case quarticEaseInOut
// Quartic easing p^5
case quinticEaseIn
case quinticEaseOut
case quinticEaseInOut
// Sine wave easing sin(p * PI/2)
case sineEaseIn
case sineEaseOut
case sineEaseInOut
// Circular easing sqrt(1 - p^2)
case circularEaseIn
case circularEaseOut
case circularEaseInOut
// Exponential easing base 2
case exponentialEaseIn
case exponentialEaseOut
case exponentialEaseInOut
// Exponentially-damped sine wave easing
case elasticEaseIn
case elasticEaseOut
case elasticEaseInOut
// Overshooting cubic easing
case backEaseIn
case backEaseOut
case backEaseInOut
// Exponentially-decaying bounce easing
case bounceEaseIn
case bounceEaseOut
case bounceEaseInOut
public func value(at progress: Float) -> Float {
switch self {
case .linear:
return linearInterpolation(p: progress)
case .quadraticEaseIn:
return quadraticEaseInValue(p: progress)
case .quadraticEaseOut:
return quadraticEaseOutValue(p: progress)
case .quadraticEaseInOut:
return quadraticEaseInOutValue(p: progress)
case .cubicEaseIn:
return cubicEaseInValue(p: progress)
case .cubicEaseOut:
return cubicEaseOutValue(p: progress)
case .cubicEaseInOut:
return cubicEaseInOutValue(p: progress)
case .quarticEaseIn:
return quarticEaseInValue(p: progress)
case .quarticEaseOut:
return quarticEaseOutValue(p: progress)
case .quarticEaseInOut:
return quarticEaseInOutValue(p: progress)
case .quinticEaseIn:
return quinticEaseInValue(p: progress)
case .quinticEaseOut:
return quinticEaseOutValue(p: progress)
case .quinticEaseInOut:
return quinticEaseInOutValue(p: progress)
case .sineEaseIn:
return sineEaseInValue(p: progress)
case .sineEaseOut:
return sineEaseOutValue(p: progress)
case .sineEaseInOut:
return sineEaseInOutValue(p: progress)
case .circularEaseIn:
return circularEaseInValue(p: progress)
case .circularEaseOut:
return circularEaseOutValue(p: progress)
case .circularEaseInOut:
return circularEaseInOutValue(p: progress)
case .exponentialEaseIn:
return exponentialEaseInValue(p: progress)
case .exponentialEaseOut:
return exponentialEaseOutValue(p: progress)
case .exponentialEaseInOut:
return exponentialEaseInOutValue(p: progress)
case .elasticEaseIn:
return elasticEaseInValue(p: progress)
case .elasticEaseOut:
return elasticEaseOutValue(p: progress)
case .elasticEaseInOut:
return elasticEaseInOutValue(p: progress)
case .backEaseIn:
return backEaseInValue(p: progress)
case .backEaseOut:
return backEaseOutValue(p: progress)
case .backEaseInOut:
return backEaseInOutValue(p: progress)
case .bounceEaseIn:
return bounceEaseInValue(p: progress)
case .bounceEaseOut:
return bounceEaseOutValue(p: progress)
case .bounceEaseInOut:
return bounceEaseInOutValue(p: progress)
}
}
private func linearInterpolation(p: Float) -> Float {
return p
}
private func quadraticEaseInValue(p: Float) -> Float {
return p * p
}
private func quadraticEaseOutValue(p: Float) -> Float {
return -(p * (p - 2))
}
private func quadraticEaseInOutValue(p: Float) -> Float {
if p < 0.5 {
return 2 * p * p
} else {
return (-2 * p * p) + (4 * p) - 1
}
}
private func cubicEaseInValue(p: Float) -> Float {
return p * p * p
}
private func cubicEaseOutValue(p: Float) -> Float {
let f = (p - 1)
return f * f * f + 1
}
private func cubicEaseInOutValue(p: Float) -> Float {
if p < 0.5 {
return 4 * p * p * p
} else {
let f = ((2 * p) - 2)
return 0.5 * f * f * f + 1
}
}
private func quarticEaseInValue(p: Float) -> Float {
return p * p * p * p
}
private func quarticEaseOutValue(p: Float) -> Float {
let f = (p - 1)
return f * f * f * (1 - p) + 1
}
private func quarticEaseInOutValue(p: Float) -> Float {
if p < 0.5 {
return 8 * p * p * p * p
} else {
let f = (p - 1)
return -8 * f * f * f * f + 1
}
}
private func quinticEaseInValue(p: Float) -> Float {
return p * p * p * p * p
}
private func quinticEaseOutValue(p: Float) -> Float {
let f = (p - 1)
return f * f * f * f * f + 1
}
private func quinticEaseInOutValue(p: Float) -> Float {
if p < 0.5 {
return 16 * p * p * p * p * p
} else {
let f = ((2 * p) - 2)
return 0.5 * f * f * f * f * f + 1
}
}
private func sineEaseInValue(p: Float) -> Float {
return sin((p - 1) * Float.pi) + 1
}
private func sineEaseOutValue(p: Float) -> Float {
return sin(p * Float.pi / 2)
}
private func sineEaseInOutValue(p: Float) -> Float {
return 0.5 * (1 - cos(p * Float.pi))
}
private func circularEaseInValue(p: Float) -> Float {
return 1 - sqrt(1 - (p * p))
}
private func circularEaseOutValue(p: Float) -> Float {
return sqrt((2 - p) * p)
}
private func circularEaseInOutValue(p: Float) -> Float {
if p < 0.5 {
return 0.5 * (1 - sqrt(1 - 4 * (p * p)))
} else {
return 0.5 * (sqrt(-((2 * p) - 3) * ((2 * p) - 1)) + 1)
}
}
private func exponentialEaseInValue(p: Float) -> Float {
return (p == 0.0) ? p : pow(2, 10 * (p - 1))
}
private func exponentialEaseOutValue(p: Float) -> Float {
return (p == 1.0) ? p : 1 - pow(2, -10 * p)
}
private func exponentialEaseInOutValue(p: Float) -> Float {
if p == 0.0 || p == 1.0 {
return p
}
if p < 0.5 {
return 0.5 * pow(2, (20 * p) - 10)
} else {
return -0.5 * pow(2, (-20 * p) + 10) + 1
}
}
private func elasticEaseInValue(p: Float) -> Float {
return sin(13 * Float.pi / 2 * p) * pow(2, 10 * (p - 1))
}
private func elasticEaseOutValue(p: Float) -> Float {
return sin(-13 * Float.pi / 2 * (p + 1)) * pow(2, -10 * p) + 1
}
private func elasticEaseInOutValue(p: Float) -> Float {
if p < 0.5 {
return 0.5 * sin(13 * Float.pi / 2 * (2 * p)) * pow(2, 10 * ((2 * p) - 1))
} else {
return 0.5 * (sin(-13 * Float.pi / 2 * ((2 * p - 1) + 1)) * pow(2, -10 * (2 * p - 1)) + 2)
}
}
private func backEaseInValue(p: Float) -> Float {
return p * p * p - p * sin(p * Float.pi)
}
private func backEaseOutValue(p: Float) -> Float {
let f = (1 - p);
return 1 - (f * f * f - f * sin(f * Float.pi))
}
private func backEaseInOutValue(p: Float) -> Float {
if p < 0.5 {
let f = 2 * p
return 0.5 * (f * f * f - f * sin(f * Float.pi))
} else {
let f = (1 - (2 * p - 1))
return 0.5 * (1 - (f * f * f - f * sin(f * Float.pi))) + 0.5
}
}
private func bounceEaseInValue(p: Float) -> Float {
return 1 - bounceEaseOutValue(p: 1 - p)
}
private func bounceEaseOutValue(p: Float) -> Float {
if p < 4/11.0 {
return (121 * p * p)/16.0
} else if p < 8/11.0 {
return (363/40.0 * p * p) - (99/10.0 * p) + 17/5.0
} else if p < 9/10.0 {
return (4356/361.0 * p * p) - (35442/1805.0 * p) + 16061/1805.0
} else {
return (54/5.0 * p * p) - (513/25.0 * p) + 268/25.0
}
}
private func bounceEaseInOutValue(p: Float) -> Float {
if p < 0.5 {
return 0.5 * bounceEaseInValue(p: p * 2)
} else {
return 0.5 * bounceEaseOutValue(p: p * 2 - 1) + 0.5
}
}
}