forked from gsempe/JFCommon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJFPropertyAnimator.swift
483 lines (388 loc) · 19.5 KB
/
JFPropertyAnimator.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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
//
// JFPropertyAnimator.swift
// JFCommon
//
// Created by Jason Fuerstenberg on 2014-06-04.
// Copyright (c) 2014 Jay Fuerstenberg Creative. All rights reserved.
//
// http://www.jayfuerstenberg.com
// jay@jayfuerstenberg.com
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import Foundation
import UIKit // For getting CGPoint type. TODO: find way to let this work on both iOS and OSX
// NOTE: Do not directly manipulate the below shared instance
var __JFPropertyAnimator_sharedInstance__ : AnyObject?
/*
* A manager of animations which advances them once per run-loop.
* Although not required for use with the JFPropertyAnimator class,
* it abstracts away much of the boilerplate code for animating
* objects and their properties.
*
* Usage Example:
* JFPropertyAnimator.animateObject(animatableObject,
* propertyId: JFPropertyAnimator.PropertyAlpha,
* startValue: 0.0,
* endValue: 1.0,
* easeIn: true,
* easeOut: true,
* duration: 1.0,
* playCount: JFPropertyAnimator.PlayCountInfinite,
* completion: {(animatable: JFPropertyAnimatable, propertyId: UInt, playLoop: Int) in
* println("completed the animation loop #\(playLoop)")
* })
*/
class JFPropertyAnimator {
// Some useful constants for use with the animator.
class var PlayCountInfinite: Int { return 0 }
class var ThirtyFps: Float { return 30.0 }
class var OneSecondDuration: NSTimeInterval { return 1.0 }
// Various helpful property types
class var PropertyReserved: UInt { return 0 }
class var PropertyXPosition: UInt { return 1 }
class var PropertyYPosition: UInt { return 2 }
class var PropertyZPosition: UInt { return 3 }
class var PropertyRed: UInt { return 11 }
class var PropertyGreen: UInt { return 12 }
class var PropertyBlue: UInt { return 13 }
class var PropertyAlpha: UInt { return 14 }
class var PropertyScale: UInt { return 21 }
class var PropertyXScale: UInt { return 22 }
class var PropertyYScale: UInt { return 23 }
class var PropertyZScale: UInt { return 24 }
class var PropertyXAngle: UInt { return 31 }
class var PropertyYAngle: UInt { return 32 }
class var PropertyZAngle: UInt { return 33 }
class var PropertySpeed: UInt { return 41 }
class var PropertyAcceleration: UInt { return 42 }
// NOTE: Custom animatable properties not covered by the above should begin from the below value
class var PropertyUserDefined: UInt {return 10000 }
// The array of managed animations which will be advanced in the run loop.
var managedAnimations: NSMutableArray = NSMutableArray(capacity: 100)
// The array of animations to remove from management (usually after finishing animation).
@lazy var animationsToRemove: NSMutableArray = NSMutableArray(capacity: 10)
/*
* The FPS of the app.
* This will help direct the animation speed.
* It is assumed the app will maintain the same FPS throughout its lifecycle.
*/
var fps: Float = JFPropertyAnimator.ThirtyFps
/*
* The flag to denote whether the animator is paused.
*
* NOTE: A paused animator will simply neglect to advance its managed animations.
* It will not assign those animations to a paused state.
*/
var paused: Bool = false
/*
* Returns the shared instance of the animator.
*/
class func sharedAnimator() -> JFPropertyAnimator {
if (__JFPropertyAnimator_sharedInstance__ == nil) {
__JFPropertyAnimator_sharedInstance__ = JFPropertyAnimator()
}
return __JFPropertyAnimator_sharedInstance__ as JFPropertyAnimator
}
/*
* Adds the provided animation to this animator.
*
* NOTE: It is more typical to call the animateObject... family of class methods
* than it is to manually add an animation via this method.
*/
func addManagedAnimation(animation: JFPropertyAnimation) {
var target: JFPropertyAnimatable = animation.target
var propertyId: UInt = animation.propertyId
var existingAnimation: JFPropertyAnimation? = animationForAnimatable(target, propertyId: propertyId)
if (existingAnimation != nil) {
managedAnimations.removeObject(existingAnimation)
}
managedAnimations.addObject(animation)
}
/*
* Returns the animation for the animatable object plus its animated property pair.
* If the object and property pair is not managed by this animator nil is returned.
*/
func animationForAnimatable(animatable: JFPropertyAnimatable, propertyId: UInt) -> JFPropertyAnimation? {
for animation : AnyObject in managedAnimations {
var anim: JFPropertyAnimation = animation as JFPropertyAnimation
var target: JFPropertyAnimatable = anim.target
if (!target.isEqualTo(animatable)) {
continue
}
var animationPropertyId: UInt = anim.propertyId
if (animationPropertyId != propertyId) {
continue
}
return anim
}
return nil
}
class func advanceManagedAnimations() {
var sharedAnimator: JFPropertyAnimator = JFPropertyAnimator.sharedAnimator()
sharedAnimator.innerAdvanceManagedAnimations()
}
func innerAdvanceManagedAnimations() {
if (paused) {
return
}
var copy: NSMutableArray = managedAnimations.mutableCopy() as NSMutableArray
for anim : AnyObject in copy {
var animation: JFPropertyAnimation = anim as JFPropertyAnimation
animation.advance()
var animationAdvancementIndex: Int
var hasReachedEnd: Bool = animation.hasReachedEnd()
if (!hasReachedEnd) {
animationAdvancementIndex = animation.animationAdvancementIndex
var currentValue: Float = animation.animationAdvancementArray[animationAdvancementIndex] as Float
animation.changeHandler(currentValue: currentValue)
} else {
var target: JFPropertyAnimatable = animation.target
var propertyId: UInt = animation.propertyId
animation.completionHandler(object: target, propertyId: propertyId, playLoop: animation.playIndex)
if (animation.playCount == 0 || animation.playIndex < animation.playCount) {
animation.repeat()
continue
}
animationsToRemove.addObject(animation)
}
}
if (animationsToRemove.count > 0) {
managedAnimations.removeObjectsInArray(animationsToRemove)
animationsToRemove.removeAllObjects()
}
}
class func animateObject(object: JFPropertyAnimatable, propertyId: UInt, startValue: Float, endValue: Float, easeIn: Bool, easeOut: Bool, duration: NSTimeInterval, playCount: Int) -> JFPropertyAnimation? {
return JFPropertyAnimator.animateObject(object,
propertyId: propertyId,
startValue: startValue,
endValue: endValue,
easeIn: easeIn,
easeOut: easeOut,
duration: duration,
playCount: playCount,
change: {(currentValue: Float) in},
completion: {(animatable: JFPropertyAnimatable, propertyId: UInt, playLoop: Int) in})
}
class func animateObject(object: JFPropertyAnimatable, propertyId: UInt, endValue: Float, easeIn: Bool, easeOut: Bool, duration: NSTimeInterval, playCount: Int) -> JFPropertyAnimation? {
return JFPropertyAnimator.animateObject(object,
propertyId: propertyId,
startValue: Float.NaN,
endValue: endValue,
easeIn: easeIn,
easeOut: easeOut,
duration: duration,
playCount: playCount,
change: {(currentValue: Float) in},
completion: {(animatable: JFPropertyAnimatable, propertyId: UInt, playLoop: Int) in})
}
class func animateObject(object: JFPropertyAnimatable, propertyId: UInt, startValue: Float, endValue: Float, easeIn: Bool, easeOut: Bool, duration: NSTimeInterval, playCount: Int, change: (Float) -> ()) -> JFPropertyAnimation? {
return JFPropertyAnimator.animateObject(object,
propertyId: propertyId,
startValue: startValue,
endValue: endValue,
easeIn: easeIn,
easeOut: easeOut,
duration: duration,
playCount: playCount,
change: change,
completion: {(animatable: JFPropertyAnimatable, propertyId: UInt, playLoop: Int) in})
}
class func animateObject(object: JFPropertyAnimatable, propertyId: UInt, endValue: Float, easeIn: Bool, easeOut: Bool, duration: NSTimeInterval, playCount: Int, change: (Float) -> (), completion: (JFPropertyAnimatable, UInt, Int) -> ()) -> JFPropertyAnimation? {
return JFPropertyAnimator.animateObject(object,
propertyId: propertyId,
startValue: Float.NaN,
endValue: endValue,
easeIn: easeIn,
easeOut: easeOut,
duration: duration,
playCount: playCount,
change: change,
completion: completion)
}
class func animateObject(object: JFPropertyAnimatable, propertyId: UInt, endValue: Float, easeIn: Bool, easeOut: Bool, duration: NSTimeInterval, playCount: Int, completion: (JFPropertyAnimatable, UInt, Int) -> ()) -> JFPropertyAnimation? {
return JFPropertyAnimator.animateObject(object,
propertyId: propertyId,
startValue: Float.NaN,
endValue: endValue,
easeIn: easeIn,
easeOut: easeOut,
duration: duration,
playCount: playCount,
change: {(currentValue: Float) in},
completion: completion)
}
class func animateObject(object: JFPropertyAnimatable, propertyId: UInt, startValue: Float, endValue: Float, easeIn: Bool, easeOut: Bool, duration: NSTimeInterval, playCount: Int, completion: (JFPropertyAnimatable, UInt, Int) -> ()) -> JFPropertyAnimation? {
return JFPropertyAnimator.animateObject(object,
propertyId: propertyId,
startValue: startValue,
endValue: endValue,
easeIn: easeIn,
easeOut: easeOut,
duration: duration,
playCount: playCount,
change: {(currentValue: Float) in},
completion: completion)
}
class func animateObject(object: JFPropertyAnimatable, propertyId: UInt, startValue: Float, endValue: Float, easeIn: Bool, easeOut: Bool, duration: NSTimeInterval, playCount: Int, change: (Float) -> (), completion: (JFPropertyAnimatable, UInt, Int) -> ()) -> JFPropertyAnimation? {
var sharedAnimator: JFPropertyAnimator = JFPropertyAnimator.sharedAnimator()
var fps: Float = sharedAnimator.fps
var innerDuration: NSTimeInterval = duration
if (innerDuration <= 0.0) {
// Reset to 1 second if 0 seconds or less.
innerDuration = 1.0
}
var animationStep: Float = 0.0
var startValueForCalculation: Float
if (!startValue.isNaN) {
object.setAnimatablePropertyToNewValue(propertyId, newValue: startValue)
startValueForCalculation = startValue
} else {
startValueForCalculation = object.valueOfAnimatableProperty(propertyId)
}
if (startValueForCalculation == endValue) {
// No animation needed as values are the same.
completion(object, propertyId, 0)
return nil
}
var count: UInt = UInt((fps * Float(innerDuration)) + 1.0)
var advancementArray: NSMutableArray = NSMutableArray()
var isLinear: Bool = isLinearWithEaseIn(easeIn, easeOut: easeOut)
if (isLinear) {
// Simple calculation...
animationStep = (endValue - startValueForCalculation) / (fps * Float(innerDuration))
var val: Float = startValueForCalculation
var isNegative: Bool = animationStep < 0.0
for index in 0..count {
if (!isNegative) {
if (val > endValue) {
val = endValue
}
} else {
if (val < endValue) {
val = endValue
}
}
advancementArray.addObject(val)
val += animationStep
}
} else if (easeIn && easeOut) {
populateAdvancementArray(advancementArray,
startPoint: CGPointMake(startValue, startValue),
controlPoint1: CGPointMake(startValue, endValue),
controlPoint2: CGPointMake(endValue, startValue),
endPoint: CGPointMake(endValue, endValue),
granularity: count)
} else if (easeIn) {
populateAdvancementArray(advancementArray,
startPoint: CGPointMake(startValue, startValue),
controlPoint1: CGPointMake(startValue, endValue),
controlPoint2: CGPointMake(startValue, endValue),
endPoint: CGPointMake(endValue, endValue),
granularity: count)
} else if (easeOut) {
populateAdvancementArray(advancementArray,
startPoint: CGPointMake(startValue, startValue),
controlPoint1: CGPointMake(endValue, startValue),
controlPoint2: CGPointMake(endValue, startValue),
endPoint: CGPointMake(endValue, endValue),
granularity: count)
}
var existingAnimation: JFPropertyAnimation? = sharedAnimator.animationForAnimatable(object, propertyId: propertyId)
if (existingAnimation != nil) {
// Stop the existing animation before adding it again...
stopAnimatingObject(object, propertyId: propertyId)
}
// Create the animation...
var animation: JFPropertyAnimation = JFPropertyAnimation(target: object)
animation.propertyId = propertyId
animation.playCount = playCount
animation.animationAdvancementArray = advancementArray
animation.changeHandler = change
animation.completionHandler = completion
// Add the animation to the managed array...
sharedAnimator.addManagedAnimation(animation)
return animation
}
class func animateObject(object: JFPropertyAnimatable, propertyId: UInt, advancementArray: NSMutableArray, playCount: Int, change: (Float) -> (), completion: (JFPropertyAnimatable, UInt, Int) -> ()) -> JFPropertyAnimation? {
var animation: JFPropertyAnimation = JFPropertyAnimation(target: object)
animation.propertyId = propertyId
animation.playCount = playCount
animation.animationAdvancementArray = advancementArray
animation.changeHandler = change
animation.completionHandler = completion
var sharedAnimator: JFPropertyAnimator = JFPropertyAnimator.sharedAnimator()
sharedAnimator.addManagedAnimation(animation)
return animation
}
class func stopAnimatingObject(object: JFPropertyAnimatable, propertyId: UInt) {
var sharedAnimator: JFPropertyAnimator = JFPropertyAnimator.sharedAnimator()
sharedAnimator.innerStopAnimatingObject(object, propertyId: propertyId, value: Float.NaN)
}
class func stopAnimatingObject(object: JFPropertyAnimatable, propertyId: UInt, finalValue: Float) {
var sharedAnimator: JFPropertyAnimator = JFPropertyAnimator.sharedAnimator()
sharedAnimator.innerStopAnimatingObject(object, propertyId: propertyId, value: finalValue)
}
func innerStopAnimatingObject(object: JFPropertyAnimatable, propertyId: UInt, value: Float) {
for anim: AnyObject in managedAnimations {
var animation: JFPropertyAnimation = anim as JFPropertyAnimation
var target: JFPropertyAnimatable = animation.target
if (!object.isEqualTo(target)) {
continue
}
var animationPropertyId: UInt = animation.propertyId
if (animationPropertyId != propertyId) {
continue
}
animation.stop()
if (!value.isNaN) {
object.setAnimatablePropertyToNewValue(propertyId, newValue: value)
}
animationsToRemove.addObject(animation)
}
if (animationsToRemove.count > 0) {
managedAnimations.removeObjectsInArray(animationsToRemove)
animationsToRemove.removeAllObjects()
}
}
class func pause() {
JFPropertyAnimator.sharedAnimator().paused = true
}
class func unpause() {
JFPropertyAnimator.sharedAnimator().paused = false
}
/*
* Returns true if the ease-in and ease-out flags denote that a given
* animation is linear in its advancement.
*/
class func isLinearWithEaseIn(easeIn: Bool, easeOut: Bool) -> Bool {
return !easeIn && !easeOut
}
/*
* Generates the ease-in, ease-out curve path given the start, control and end points assigned.
* The granularity determines the number of elements that will be present in the resulting array.
*/
class func populateAdvancementArray(advancementArray: NSMutableArray, startPoint: CGPoint, controlPoint1: CGPoint, controlPoint2: CGPoint, endPoint: CGPoint, granularity: UInt) {
var step: Float = (1.0 / (Float(granularity) - 1.0))
var t: Float = 0.0
for (var loop: UInt = 0; loop < granularity - UInt(1); loop++, t += step) {
var c: CGPoint = CGPointMake(3.0 * (controlPoint1.x - startPoint.x), (controlPoint1.y - startPoint.y))
var b: CGPoint = CGPointMake(3.0 * (controlPoint2.x - controlPoint1.x) - c.x, 3.0 * (controlPoint2.y - controlPoint1.y) - c.y)
var a: CGPoint = CGPointMake(endPoint.x - startPoint.x - c.x - b.x, endPoint.y - startPoint.y - c.y - b.y)
var t2: Float = t * t
var t3: Float = t2 * t
var temp: CGPoint = CGPointMake((a.x * t3) + (b.x * t2) + (c.x * t) + startPoint.x, (a.y * t3) + (b.y * t2) + (c.y * t) + startPoint.y)
advancementArray[Int(loop)] = temp.x
}
advancementArray[Int(granularity - 1)] = endPoint.x
}
}