forked from jayfuerstenberg/JFCommon
-
Notifications
You must be signed in to change notification settings - Fork 1
/
JFPropertyAnimator.m
executable file
·522 lines (407 loc) · 15.8 KB
/
JFPropertyAnimator.m
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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
//
// JFPropertyAnimator.m
// JFCommon
//
// Created by Jason Fuerstenberg on 2013/09/17.
// Copyright (c) 2013 Jason Fuerstenberg. 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 "JFPropertyAnimator.h"
// Pseudo-singleton shared instance.
JFPropertyAnimator *__JFPropertyAnimator__sharedSingletonInstance__ = nil;
@implementation JFPropertyAnimator
#pragma mark - Properties
@synthesize fps = _fps;
@synthesize paused = _paused;
#pragma mark - Object lifecycle methods
/*
* Returns the shared animator instance.
*/
+ (id) sharedAnimator {
if (__JFPropertyAnimator__sharedSingletonInstance__ == nil) {
__JFPropertyAnimator__sharedSingletonInstance__ = [[JFPropertyAnimator alloc] init];
}
return __JFPropertyAnimator__sharedSingletonInstance__;
}
/*
* Releases the shared animator instance.
*/
+ (void) releaseSharedAnimator {
[__JFPropertyAnimator__sharedSingletonInstance__ release];
__JFPropertyAnimator__sharedSingletonInstance__ = nil;
}
- (id) init {
self = [super init];
if (self != nil) {
_fps = JFPropertyAnimator30Fps; // Assume 30fps unless told otherwise.
[self lazyInitManagedAnimations];
[self lazyInitAnimationsToRemove];
}
return self;
}
- (void) dealloc {
[_managedAnimations release];
[_animationsToRemove release];
[super dealloc];
}
#pragma mark - Lazy initialization methods
- (void) lazyInitManagedAnimations {
if (_managedAnimations != nil) {
return;
}
_managedAnimations = [[NSMutableArray alloc] initWithCapacity: 100];
}
- (void) lazyInitAnimationsToRemove {
if (_animationsToRemove != nil) {
return;
}
_animationsToRemove = [[NSMutableArray alloc] initWithCapacity: 10];
}
#pragma mark - Animation methods
/*
* 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.
*/
- (void) addManagedAnimation: (JFPropertyAnimation *) animation {
if (animation == nil) {
return;
}
id <JFPropertyAnimatable> target = [animation target];
NSUInteger propertyId = [animation propertyId];
JFPropertyAnimation *existingAnimation = [self animationForAnimatable: target
property: propertyId];
if (existingAnimation != nil) {
// Need to remove the existing animation if adding a new one for the same object/property.
@synchronized (_managedAnimations) {
[_managedAnimations removeObject: existingAnimation];
}
}
@synchronized (_managedAnimations) {
[_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.
*/
- (JFPropertyAnimation *) animationForAnimatable: (id <JFPropertyAnimatable>) animatable property: (NSUInteger) propertyId {
if (animatable == nil) {
return nil;
}
@synchronized (_managedAnimations) {
for (JFPropertyAnimation *animation in _managedAnimations) {
id <JFPropertyAnimatable> target = [animation target];
if (target != animatable) {
continue;
}
NSUInteger animationPropertyId = [animation propertyId];
if (animationPropertyId != propertyId) {
continue;
}
return animation;
}
}
return nil;
}
/*
* Iterates through the managed animations and advances them.
*/
+ (void) advanceManagedAnimations {
JFPropertyAnimator *sharedAnimator = [JFPropertyAnimator sharedAnimator];
[sharedAnimator innerAdvanceManagedAnimations];
}
/*
* Performs the actual advancement.
* Do not call this method outside of this class.
*/
- (void) innerAdvanceManagedAnimations {
if (_paused) {
return;
}
NSMutableArray *copy = nil;
@synchronized (_managedAnimations) {
copy = [_managedAnimations copy];
}
@synchronized (copy) {
for (JFPropertyAnimation *animation in copy) {
[animation advance];
JFPropertyAnimatorAnimateChangeHandler changeHandler = [animation changeHandler];
if (changeHandler != 0) {
NSUInteger index = [animation playIndex];
CGFloat currentValue = [[animation animationAdvancementArray] valueAtIndex: index];
changeHandler(currentValue);
}
if ([animation hasReachedEnd]) {
JFPropertyAnimatorAnimateCompletionHandler completionHandler = [animation completionHandler];
if (completionHandler != 0) {
id <JFPropertyAnimatable> target = [animation target];
NSUInteger propertyId = [animation propertyId];
completionHandler(target, propertyId, 0);
}
NSUInteger playCount = [animation playCount];
NSUInteger playIndex = [animation playIndex];
if (playCount == 0 || playIndex < playCount) {
// This is a never-ending animation or a limited one with some plays left.
[animation repeat];
continue;
}
[_animationsToRemove addObject: animation];
}
}
}
@synchronized (_managedAnimations) {
if ([_animationsToRemove count] > 0) {
[_managedAnimations removeObjectsInArray: _animationsToRemove];
[_animationsToRemove removeAllObjects];
}
}
}
/*
* Animates the provided object/property.
*/
+ (JFPropertyAnimation *) animateObject: (id <JFPropertyAnimatable>) object property: (NSUInteger) propertyId to: (CGFloat) endValue easeIn: (BOOL) easeIn easeOut: (BOOL) easeOut overDuration: (NSTimeInterval) duration playCount: (NSUInteger) playCount change: (JFPropertyAnimatorAnimateChangeHandler) changeHandler completion: (JFPropertyAnimatorAnimateCompletionHandler) completionHandler {
return [JFPropertyAnimator animateObject: object
property: propertyId
from: NAN // Go with the object's current vale.
to: endValue
easeIn: easeIn
easeOut: easeOut
overDuration: duration
playCount: playCount
change: changeHandler
completion: completionHandler];
}
/*
* The very specific method to call for animating the provided object's property.
*/
+ (JFPropertyAnimation *) animateObject: (id <JFPropertyAnimatable>) object property: (NSUInteger) propertyId from: (CGFloat) startValue to: (CGFloat) endValue easeIn: (BOOL) easeIn easeOut: (BOOL) easeOut overDuration: (NSTimeInterval) duration playCount: (NSUInteger) playCount change: (JFPropertyAnimatorAnimateChangeHandler) changeHandler completion: (JFPropertyAnimatorAnimateCompletionHandler) completionHandler {
if (object == nil || ![object conformsToProtocol: @protocol(JFPropertyAnimatable)]) {
// Not a valid object.
return nil;
}
JFPropertyAnimator *sharedAnimator = [JFPropertyAnimator sharedAnimator];
CGFloat fps = [sharedAnimator fps];
if (fps == 0.0f) {
// Invalid! Reset to 30.
fps = JFPropertyAnimator30Fps;
}
if (duration <= 0.0f) {
// Reset to 1 second if 0 seconds or less.
duration = JFPropertyAnimator1SecondDuration;
}
CGFloat animationStep = 0.0f;
CGFloat startValueForCalculation;
if (!isnan(startValue)) {
// The start value is valid and should be assigned now...
[object setAnimatableProperty: propertyId
toNewValue: startValue];
startValueForCalculation = startValue;
} else {
// The caller wants to go with the object's current value as the start value so retrieve that now...
startValueForCalculation = [object valueOfAnimatableProperty: propertyId];
}
if (startValueForCalculation == endValue) {
// No animation needed as values are the same.
return nil;
}
NSUInteger count = (NSUInteger) (fps * duration) + 1;
JFPropertyAnimationAdvancementArray *advancementArray = [JFPropertyAnimationAdvancementArray advancementArrayWithElementCount: count];
BOOL isLinear = [JFPropertyAnimator isLinearWithEaseIn: easeIn
easeOut: easeOut];
if (isLinear) {
// Simple calculation...
animationStep = (endValue - startValueForCalculation) / (fps * duration);
CGFloat val = startValueForCalculation;
BOOL isNegative = animationStep < 0.0f;
for (NSUInteger index = 0; index < count; index++) {
if (!isNegative) {
if (val > endValue) {
val = endValue;
}
} else {
if (val < endValue) {
val = endValue;
}
}
[advancementArray setValue: val
atIndex: index];
val += animationStep;
}
} else if (easeIn && easeOut) {
[JFPropertyAnimator populateAdvancementArray: advancementArray
withStartPoint: CGPointMake(startValue, startValue)
controlPoint1: CGPointMake(startValue, endValue)
controlPoint2: CGPointMake(endValue, startValue)
endPoint: CGPointMake(endValue, endValue)
granularity: count];
} else if (easeIn) {
[JFPropertyAnimator populateAdvancementArray: advancementArray
withStartPoint: CGPointMake(startValue, startValue)
controlPoint1: CGPointMake(startValue, endValue)
controlPoint2: CGPointMake(startValue, endValue)
endPoint: CGPointMake(endValue, endValue)
granularity: count];
} else if (easeOut) {
[JFPropertyAnimator populateAdvancementArray: advancementArray
withStartPoint: CGPointMake(startValue, startValue)
controlPoint1: CGPointMake(endValue, startValue)
controlPoint2: CGPointMake(endValue, startValue)
endPoint: CGPointMake(endValue, endValue)
granularity: count];
}
// Create the animation...
JFPropertyAnimation *animation = [JFPropertyAnimation propertyAnimation];
[animation setTarget: object];
[animation setPropertyId: propertyId];
[animation setPlayCount: playCount];
[animation setAnimationAdvancementArray: advancementArray];
[animation setChangeHandler: changeHandler];
[animation setCompletionHandler: completionHandler];
// Add the animation to the managed array...
[sharedAnimator addManagedAnimation: animation];
return animation;
}
/*
* Animates the provided object using the custom advancement array.
*/
+ (JFPropertyAnimation *) animateObject: (id <JFPropertyAnimatable>) object property: (NSUInteger) propertyId withAdvancementArray: (JFPropertyAnimationAdvancementArray *) advancementArray playCount: (NSUInteger) playCount change: (JFPropertyAnimatorAnimateChangeHandler) changeHandler completion: (JFPropertyAnimatorAnimateCompletionHandler) completionHandler {
if (object == nil || ![object conformsToProtocol: @protocol(JFPropertyAnimatable)]) {
// Not a valid object.
return nil;
}
if (advancementArray == nil) {
// Not a valid array.
return nil;
}
// Create the animation...
JFPropertyAnimation *animation = [JFPropertyAnimation propertyAnimation];
[animation setTarget: object];
[animation setPropertyId: propertyId];
[animation setPlayCount: playCount];
[animation setAnimationAdvancementArray: advancementArray];
[animation setChangeHandler: changeHandler];
[animation setCompletionHandler: completionHandler];
// Add the animation to the managed array...
JFPropertyAnimator *sharedAnimator = [JFPropertyAnimator sharedAnimator];
[sharedAnimator addManagedAnimation: animation];
return animation;
}
/*
* Stops managing the object/property if it is managed.
*/
+ (void) stopAnimatingObject: (id <JFPropertyAnimatable>) object property: (NSUInteger) propertyId {
JFPropertyAnimator *sharedAnimator = [JFPropertyAnimator sharedAnimator];
[sharedAnimator innerStopAnimatingObject: object
property: propertyId];
}
/*
* NOTE: Do not call this method from outside this class.
*/
- (void) innerStopAnimatingObject: (id <JFPropertyAnimatable>) object property: (NSUInteger) propertyId {
JFPropertyAnimation *animation = [self animationForAnimatable: object
property: propertyId];
if (animation == nil) {
return;
}
[animation stop];
@synchronized (_managedAnimations) {
[_animationsToRemove addObject: animation];
if ([_animationsToRemove count] > 0) {
[_managedAnimations removeObjectsInArray: _animationsToRemove];
[_animationsToRemove removeAllObjects];
}
}
}
+ (void) stopAnimatingObject: (id <JFPropertyAnimatable>) object property: (NSUInteger) propertyId setFinalValueTo: (CGFloat) value {
JFPropertyAnimator *sharedAnimator = [JFPropertyAnimator sharedAnimator];
[sharedAnimator innerStopAnimatingObject: object
property: propertyId
setFinalValueTo: value];
}
/*
* NOTE: Do not call this method from outside this class.
*/
- (void) innerStopAnimatingObject: (id <JFPropertyAnimatable>) object property: (NSUInteger) propertyId setFinalValueTo: (CGFloat) value {
@synchronized (_managedAnimations) {
for (JFPropertyAnimation *animation in _managedAnimations) {
id <JFPropertyAnimatable> target = [animation target];
if (target != object) {
continue;
}
NSUInteger animationPropertyId = [animation propertyId];
if (animationPropertyId != propertyId) {
continue;
}
// Matching animation found!
[animation stop];
[object setAnimatableProperty: propertyId
toNewValue: value];
[_animationsToRemove addObject: animation];
}
if ([_animationsToRemove count] > 0) {
[_managedAnimations removeObjectsInArray: _animationsToRemove];
[_animationsToRemove removeAllObjects];
}
}
}
/*
* Pauses the shared animator.
*/
+ (void) pause {
[[JFPropertyAnimator sharedAnimator] setPaused: YES];
}
/*
* Unpauses the shared animator.
*/
+ (void) unpause {
[[JFPropertyAnimator sharedAnimator] setPaused: NO];
}
/*
* Returns YES if the ease-in and ease-out flags denote that a given
* animation is linear in its advancement.
*/
+ (BOOL) isLinearWithEaseIn: (BOOL) easeIn easeOut: (BOOL) easeOut {
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.
*/
+ (void) populateAdvancementArray: (JFPropertyAnimationAdvancementArray *) advancementArray withStartPoint: (CGPoint) startPoint controlPoint1: (CGPoint) controlPoint1 controlPoint2: (CGPoint) controlPoint2 endPoint: (CGPoint) endPoint granularity: (NSUInteger) granularity {
double step = 1.0 / ((double) granularity - 1.0);
double t = 0.0;
for (SInt32 loop = 0; loop < granularity - 1; loop++, t += step) {
CGPoint a, b, c;
CGPoint temp;
c.x = 3.0f * (controlPoint1.x - startPoint.x);
c.y = 3.0f * (controlPoint1.y - startPoint.y);
b.x = 3.0f * (controlPoint2.x - controlPoint1.x) - c.x;
b.y = 3.0f * (controlPoint2.y - controlPoint1.y) - c.y;
a.x = endPoint.x - startPoint.x - c.x - b.x;
a.y = endPoint.y - startPoint.y - c.y - b.y;
double t2 = t * t;
double t3 = t2 * t;
temp.x = (a.x * t3) + (b.x * t2) + (c.x * t) + startPoint.x;
temp.y = (a.y * t3) + (b.y * t2) + (c.y * t) + startPoint.y;
[advancementArray setValue: temp.x
atIndex: loop];
}
[advancementArray setValue: endPoint.x
atIndex: granularity - 1];
}
@end