-
Notifications
You must be signed in to change notification settings - Fork 63
/
PRTween.m
executable file
·726 lines (568 loc) · 37.4 KB
/
PRTween.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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
#import "PRTween.h"
#define kPRTweenFramerate 1.0/60
@implementation PRTweenPeriod
@synthesize startValue;
@synthesize endValue;
@synthesize tweenedValue;
@synthesize duration;
@synthesize delay;
@synthesize startOffset;
+ (id)periodWithStartValue:(CGFloat)aStartValue endValue:(CGFloat)anEndValue duration:(CGFloat)duration {
PRTweenPeriod *period = [PRTweenPeriod new];
period.startValue = period.tweenedValue = aStartValue;
period.endValue = anEndValue;
period.duration = duration;
period.startOffset = [[PRTween sharedInstance] timeOffset];
return period;
}
@end
@implementation PRTweenLerpPeriod
@synthesize startLerp;
@synthesize endLerp;
@synthesize tweenedLerp;
+ (id)periodWithStartValue:(NSValue*)aStartValue endValue:(NSValue*)anEndValue duration:(CGFloat)duration {
PRTweenLerpPeriod *period = [[self class] new];
period.startLerp = aStartValue;
period.tweenedLerp = aStartValue;
period.endLerp = anEndValue;
period.duration = duration;
period.startOffset = [[PRTween sharedInstance] timeOffset];
return period;
}
@end
@implementation PRTweenCGPointLerpPeriod
+ (id)periodWithStartCGPoint:(CGPoint)aStartPoint endCGPoint:(CGPoint)anEndPoint duration:(CGFloat)duration {
return [PRTweenCGPointLerpPeriod periodWithStartValue:[NSValue valueWithCGPoint:aStartPoint] endValue:[NSValue valueWithCGPoint:anEndPoint] duration:duration];
}
- (CGPoint)startCGPoint { return [self.startLerp CGPointValue]; }
- (CGPoint)tweenedCGPoint { return [self.tweenedLerp CGPointValue]; }
- (CGPoint)endCGPoint { return [self.endLerp CGPointValue]; }
- (NSValue*)tweenedValueForProgress:(CGFloat)progress {
CGPoint startPoint = self.startCGPoint;
CGPoint endPoint = self.endCGPoint;
CGPoint distance = CGPointMake(endPoint.x - startPoint.x, endPoint.y - startPoint.y);
CGPoint tweenedPoint = CGPointMake(startPoint.x + distance.x * progress, startPoint.y + distance.y * progress);
return [NSValue valueWithCGPoint:tweenedPoint];
}
- (void)setProgress:(CGFloat)progress {
self.tweenedLerp = [self tweenedValueForProgress:progress];
}
@end
@implementation PRTweenCGRectLerpPeriod
+ (id)periodWithStartCGRect:(CGRect)aStartRect endCGRect:(CGRect)anEndRect duration:(CGFloat)duration {
return [PRTweenCGRectLerpPeriod periodWithStartValue:[NSValue valueWithCGRect:aStartRect] endValue:[NSValue valueWithCGRect:anEndRect] duration:duration];
}
- (CGRect)startCGRect { return [self.startLerp CGRectValue]; }
- (CGRect)tweenedCGRect { return [self.tweenedLerp CGRectValue]; }
- (CGRect)endCGRect { return [self.endLerp CGRectValue]; }
- (NSValue *)tweenedValueForProgress:(CGFloat)progress {
CGRect startRect = self.startCGRect;
CGRect endRect = self.endCGRect;
CGRect distance = CGRectMake(endRect.origin.x - startRect.origin.x, endRect.origin.y - startRect.origin.y, endRect.size.width - startRect.size.width, endRect.size.height - startRect.size.height);
CGRect tweenedRect = CGRectMake(startRect.origin.x + distance.origin.x * progress, startRect.origin.y + distance.origin.y * progress, startRect.size.width + distance.size.width * progress, startRect.size.height + distance.size.height * progress);
return [NSValue valueWithCGRect:tweenedRect];
}
- (void)setProgress:(CGFloat)progress {
self.tweenedLerp = [self tweenedValueForProgress:progress];
}
@end
@implementation PRTweenCGSizeLerpPeriod
+ (id)periodWithStartCGSize:(CGSize)aStartSize endCGSize:(CGSize)anEndSize duration:(CGFloat)duration {
return [PRTweenCGRectLerpPeriod periodWithStartValue:[NSValue valueWithCGSize:aStartSize] endValue:[NSValue valueWithCGSize:anEndSize] duration:duration];
}
- (CGSize)startCGSize { return [self.startLerp CGSizeValue]; }
- (CGSize)tweenedCGSize { return [self.tweenedLerp CGSizeValue]; }
- (CGSize)endCGSize { return [self.endLerp CGSizeValue]; }
- (NSValue *)tweenedValueForProgress:(CGFloat)progress {
CGSize startSize = self.startCGSize;
CGSize endSize = self.endCGSize;
CGSize distance = CGSizeMake(endSize.width - startSize.width, endSize.height - startSize.height);
CGSize tweenedSize = CGSizeMake(startSize.width + distance.width * progress, startSize.height + distance.height * progress);
return [NSValue valueWithCGSize:tweenedSize];
}
- (void)setProgress:(CGFloat)progress {
self.tweenedLerp = [self tweenedValueForProgress:progress];
}
@end
@interface PRTweenOperation ()
@property (nonatomic) BOOL canUseBuiltAnimation;
@end
@implementation PRTweenOperation
@synthesize period;
@synthesize target;
@synthesize updateSelector;
@synthesize completeSelector;
@synthesize timingFunction;
@synthesize boundRef;
@synthesize boundObject;
@synthesize boundGetter;
@synthesize boundSetter;
@synthesize canUseBuiltAnimation;
@synthesize override;
#if NS_BLOCKS_AVAILABLE
@synthesize updateBlock;
@synthesize completeBlock;
#endif
@end
@implementation PRTweenCGPointLerp
+ (PRTweenOperation *)lerp:(id)object property:(NSString *)property from:(CGPoint)from to:(CGPoint)to duration:(CGFloat)duration timingFunction:(PRTweenTimingFunction)timingFunction target:(NSObject *)target completeSelector:(SEL)selector {
return [PRTween lerp:object property:property period:[PRTweenCGPointLerpPeriod periodWithStartCGPoint:from endCGPoint:to duration:duration] timingFunction:timingFunction target:target completeSelector:selector];
}
+ (PRTweenOperation *)lerp:(id)object property:(NSString *)property from:(CGPoint)from to:(CGPoint)to duration:(CGFloat)duration delay:(CGFloat)delay timingFunction:(PRTweenTimingFunction)timingFunction target:(NSObject *)target completeSelector:(SEL)selector {
PRTweenCGPointLerpPeriod *period = [PRTweenCGPointLerpPeriod periodWithStartCGPoint:from endCGPoint:to duration:duration];
period.delay = delay;
return [PRTween lerp:object property:property period:period timingFunction:timingFunction target:target completeSelector:selector];
}
+ (PRTweenOperation *)lerp:(id)object property:(NSString *)property from:(CGPoint)from to:(CGPoint)to duration:(CGFloat)duration {
return [PRTweenCGPointLerp lerp:object property:property from:from to:to duration:duration timingFunction:NULL target:nil completeSelector:NULL];
}
#if NS_BLOCKS_AVAILABLE
+ (PRTweenOperation *)lerp:(id)object property:(NSString *)property from:(CGPoint)from to:(CGPoint)to duration:(CGFloat)duration timingFunction:(PRTweenTimingFunction)timingFunction updateBlock:(PRTweenUpdateBlock)updateBlock completeBlock:(PRTweenCompleteBlock)completeBlock {
return [PRTween lerp:object property:property period:[PRTweenCGPointLerpPeriod periodWithStartCGPoint:from endCGPoint:to duration:duration] timingFunction:timingFunction updateBlock:updateBlock completeBlock:completeBlock];
}
+ (PRTweenOperation *)lerp:(id)object property:(NSString *)property from:(CGPoint)from to:(CGPoint)to duration:(CGFloat)duration delay:(CGFloat)delay timingFunction:(PRTweenTimingFunction)timingFunction updateBlock:(PRTweenUpdateBlock)updateBlock completeBlock:(PRTweenCompleteBlock)completeBlock {
PRTweenCGPointLerpPeriod *period = [PRTweenCGPointLerpPeriod periodWithStartCGPoint:from endCGPoint:to duration:duration];
[period setDelay:delay];
return [PRTween lerp:object property:property period:period timingFunction:timingFunction updateBlock:updateBlock completeBlock:completeBlock];
}
#endif
@end
@implementation PRTweenCGRectLerp
+ (PRTweenOperation *)lerp:(id)object property:(NSString *)property from:(CGRect)from to:(CGRect)to duration:(CGFloat)duration timingFunction:(PRTweenTimingFunction)timingFunction target:(NSObject *)target completeSelector:(SEL)selector {
return [PRTween lerp:object property:property period:[PRTweenCGRectLerpPeriod periodWithStartCGRect:from endCGRect:to duration:duration] timingFunction:timingFunction target:target completeSelector:selector];
}
+ (PRTweenOperation *)lerp:(id)object property:(NSString *)property from:(CGRect)from to:(CGRect)to duration:(CGFloat)duration delay:(CGFloat)delay timingFunction:(PRTweenTimingFunction)timingFunction target:(NSObject *)target completeSelector:(SEL)selector {
PRTweenCGRectLerpPeriod *period = [PRTweenCGRectLerpPeriod periodWithStartCGRect:from endCGRect:to duration:duration];
period.delay = delay;
return [PRTween lerp:object property:property period:period timingFunction:timingFunction target:target completeSelector:selector];
}
+ (PRTweenOperation *)lerp:(id)object property:(NSString *)property from:(CGRect)from to:(CGRect)to duration:(CGFloat)duration {
return [PRTweenCGRectLerp lerp:object property:property from:from to:to duration:duration timingFunction:NULL target:nil completeSelector:NULL];
}
#if NS_BLOCKS_AVAILABLE
+ (PRTweenOperation *)lerp:(id)object property:(NSString *)property from:(CGRect)from to:(CGRect)to duration:(CGFloat)duration timingFunction:(PRTweenTimingFunction)timingFunction updateBlock:(PRTweenUpdateBlock)updateBlock completeBlock:(PRTweenCompleteBlock)completeBlock {
return [PRTween lerp:object property:property period:[PRTweenCGRectLerpPeriod periodWithStartCGRect:from endCGRect:to duration:duration] timingFunction:timingFunction updateBlock:updateBlock completeBlock:completeBlock];
}
+ (PRTweenOperation *)lerp:(id)object property:(NSString *)property from:(CGRect)from to:(CGRect)to duration:(CGFloat)duration delay:(CGFloat)delay timingFunction:(PRTweenTimingFunction)timingFunction updateBlock:(PRTweenUpdateBlock)updateBlock completeBlock:(PRTweenCompleteBlock)completeBlock {
PRTweenCGRectLerpPeriod *period = [PRTweenCGRectLerpPeriod periodWithStartCGRect:from endCGRect:to duration:duration];
[period setDelay:delay];
return [PRTween lerp:object property:property period:period timingFunction:timingFunction updateBlock:updateBlock completeBlock:completeBlock];
}
#endif
@end
@implementation PRTweenCGSizeLerp
+ (PRTweenOperation *)lerp:(id)object property:(NSString *)property from:(CGSize)from to:(CGSize)to duration:(CGFloat)duration timingFunction:(PRTweenTimingFunction)timingFunction target:(NSObject *)target completeSelector:(SEL)selector {
return [PRTween lerp:object property:property period:[PRTweenCGSizeLerpPeriod periodWithStartCGSize:from endCGSize:to duration:duration] timingFunction:timingFunction target:target completeSelector:selector];
}
+ (PRTweenOperation *)lerp:(id)object property:(NSString *)property from:(CGSize)from to:(CGSize)to duration:(CGFloat)duration delay:(CGFloat)delay timingFunction:(PRTweenTimingFunction)timingFunction target:(NSObject *)target completeSelector:(SEL)selector {
PRTweenCGPointLerpPeriod *period = [PRTweenCGSizeLerpPeriod periodWithStartCGSize:from endCGSize:to duration:duration];
period.delay = delay;
return [PRTween lerp:object property:property period:period timingFunction:timingFunction target:target completeSelector:selector];
}
+ (PRTweenOperation *)lerp:(id)object property:(NSString *)property from:(CGSize)from to:(CGSize)to duration:(CGFloat)duration {
return [PRTweenCGSizeLerp lerp:object property:property from:from to:to duration:duration timingFunction:NULL target:nil completeSelector:NULL];
}
#if NS_BLOCKS_AVAILABLE
+ (PRTweenOperation *)lerp:(id)object property:(NSString *)property from:(CGSize)from to:(CGSize)to duration:(CGFloat)duration timingFunction:(PRTweenTimingFunction)timingFunction updateBlock:(PRTweenUpdateBlock)updateBlock completeBlock:(PRTweenCompleteBlock)completeBlock {
return [PRTween lerp:object property:property period:[PRTweenCGSizeLerpPeriod periodWithStartCGSize:from endCGSize:to duration:duration] timingFunction:timingFunction updateBlock:updateBlock completeBlock:completeBlock];
}
+ (PRTweenOperation *)lerp:(id)object property:(NSString *)property from:(CGSize)from to:(CGSize)to duration:(CGFloat)duration delay:(CGFloat)delay timingFunction:(PRTweenTimingFunction)timingFunction updateBlock:(PRTweenUpdateBlock)updateBlock completeBlock:(PRTweenCompleteBlock)completeBlock {
PRTweenCGSizeLerpPeriod *period = [PRTweenCGSizeLerpPeriod periodWithStartCGSize:from endCGSize:to duration:duration];
[period setDelay:delay];
return [PRTween lerp:object property:property period:period timingFunction:timingFunction updateBlock:updateBlock completeBlock:completeBlock];
}
#endif
@end
@interface PRTween ()
+ (SEL)setterFromProperty:(NSString *)property;
- (void)update;
@end
static PRTween *instance = nil;
static NSArray *animationSelectorsForCoreAnimation = nil;
static NSArray *animationSelectorsForUIView = nil;
@implementation PRTween
@synthesize timeOffset;
@synthesize defaultTimingFunction;
@synthesize useBuiltInAnimationsWhenPossible;
+ (PRTween *)sharedInstance {
if (instance == nil) {
instance = [[PRTween alloc] init];
instance.useBuiltInAnimationsWhenPossible = YES;
}
return instance;
}
+ (PRTweenOperation *)tween:(id)object property:(NSString*)property from:(CGFloat)from to:(CGFloat)to duration:(CGFloat)duration timingFunction:(PRTweenTimingFunction)timingFunction target:(NSObject*)target completeSelector:(SEL)selector {
PRTweenPeriod *period = [PRTweenPeriod periodWithStartValue:from endValue:to duration:duration];
PRTweenOperation *operation = [PRTweenOperation new];
operation.period = period;
operation.timingFunction = timingFunction;
operation.target = target;
operation.completeSelector = selector;
operation.boundObject = object;
operation.boundGetter = NSSelectorFromString([NSString stringWithFormat:@"%@", property]);
operation.boundSetter = [PRTween setterFromProperty:property];
[operation addObserver:[PRTween sharedInstance] forKeyPath:@"period.tweenedValue" options:NSKeyValueObservingOptionNew context:NULL];
[[PRTween sharedInstance] performSelector:@selector(addTweenOperation:) withObject:operation afterDelay:0];
return operation;
}
+ (PRTweenOperation *)tween:(CGFloat *)ref from:(CGFloat)from to:(CGFloat)to duration:(CGFloat)duration timingFunction:(PRTweenTimingFunction)timingFunction target:(NSObject*)target completeSelector:(SEL)selector {
PRTweenPeriod *period = [PRTweenPeriod periodWithStartValue:from endValue:to duration:duration];
PRTweenOperation *operation = [PRTweenOperation new];
operation.period = period;
operation.timingFunction = timingFunction;
operation.target = target;
operation.completeSelector = selector;
operation.boundRef = ref;
[operation addObserver:[PRTween sharedInstance] forKeyPath:@"period.tweenedValue" options:NSKeyValueObservingOptionNew context:NULL];
[[PRTween sharedInstance] performSelector:@selector(addTweenOperation:) withObject:operation afterDelay:0];
return operation;
}
+ (PRTweenOperation *)tween:(id)object property:(NSString*)property from:(CGFloat)from to:(CGFloat)to duration:(CGFloat)duration {
return [PRTween tween:object property:property from:from to:to duration:duration timingFunction:NULL target:nil completeSelector:NULL];
}
+ (PRTweenOperation *)tween:(CGFloat *)ref from:(CGFloat)from to:(CGFloat)to duration:(CGFloat)duration {
return [PRTween tween:ref from:from to:to duration:duration timingFunction:NULL target:nil completeSelector:NULL];
}
+ (PRTweenOperation *)lerp:(id)object property:(NSString *)property period:(PRTweenLerpPeriod <PRTweenLerpPeriod> *)period timingFunction:(PRTweenTimingFunction)timingFunction target:(NSObject *)target completeSelector:(SEL)selector {
//PRTweenPeriod *period = [PRTweenLerpPeriod periodWithStartValue:from endValue:to duration:duration];
PRTweenOperation *operation = [PRTweenOperation new];
operation.period = period;
operation.timingFunction = timingFunction;
operation.target = target;
operation.completeSelector = selector;
operation.boundObject = object;
operation.boundGetter = NSSelectorFromString([NSString stringWithFormat:@"%@", property]);
operation.boundSetter = [PRTween setterFromProperty:property];
[operation addObserver:[PRTween sharedInstance] forKeyPath:@"period.tweenedLerp" options:NSKeyValueObservingOptionNew context:NULL];
[[PRTween sharedInstance] performSelector:@selector(addTweenOperation:) withObject:operation afterDelay:0];
return operation;
}
#if NS_BLOCKS_AVAILABLE
+ (PRTweenOperation *)tween:(id)object property:(NSString*)property from:(CGFloat)from to:(CGFloat)to duration:(CGFloat)duration timingFunction:(PRTweenTimingFunction)timingFunction updateBlock:(PRTweenUpdateBlock)updateBlock completeBlock:(PRTweenCompleteBlock)completeBlock {
PRTweenPeriod *period = [PRTweenPeriod periodWithStartValue:from endValue:to duration:duration];
PRTweenOperation *operation = [PRTweenOperation new];
operation.period = period;
operation.timingFunction = timingFunction;
operation.updateBlock = updateBlock;
operation.completeBlock = completeBlock;
operation.boundObject = object;
operation.boundGetter = NSSelectorFromString([NSString stringWithFormat:@"%@", property]);
operation.boundSetter = [PRTween setterFromProperty:property];
[operation addObserver:[PRTween sharedInstance] forKeyPath:@"period.tweenedValue" options:NSKeyValueObservingOptionNew context:NULL];
[[PRTween sharedInstance] performSelector:@selector(addTweenOperation:) withObject:operation afterDelay:0];
return operation;
}
+ (PRTweenOperation *)tween:(CGFloat *)ref from:(CGFloat)from to:(CGFloat)to duration:(CGFloat)duration timingFunction:(PRTweenTimingFunction)timingFunction updateBlock:(PRTweenUpdateBlock)updateBlock completeBlock:(PRTweenCompleteBlock)completeBlock {
PRTweenPeriod *period = [PRTweenPeriod periodWithStartValue:from endValue:to duration:duration];
PRTweenOperation *operation = [PRTweenOperation new];
operation.period = period;
operation.timingFunction = timingFunction;
operation.updateBlock = updateBlock;
operation.completeBlock = completeBlock;
operation.boundRef = ref;
[operation addObserver:[PRTween sharedInstance] forKeyPath:@"period.tweenedValue" options:NSKeyValueObservingOptionNew context:NULL];
[[PRTween sharedInstance] performSelector:@selector(addTweenOperation:) withObject:operation afterDelay:0];
return operation;
}
+ (PRTweenOperation *)lerp:(id)object property:(NSString *)property period:(PRTweenLerpPeriod <PRTweenLerpPeriod> *)period timingFunction:(PRTweenTimingFunction)timingFunction updateBlock:(PRTweenUpdateBlock)updateBlock completeBlock:(PRTweenCompleteBlock)completeBlock {
//PRTweenPeriod *period = [PRTweenLerpPeriod periodWithStartValue:from endValue:to duration:duration];
PRTweenOperation *operation = [PRTweenOperation new];
operation.period = period;
operation.timingFunction = timingFunction;
operation.updateBlock = updateBlock;
operation.completeBlock = completeBlock;
operation.boundObject = object;
operation.boundGetter = NSSelectorFromString([NSString stringWithFormat:@"%@", property]);
operation.boundSetter = [PRTween setterFromProperty:property];
[operation addObserver:[PRTween sharedInstance] forKeyPath:@"period.tweenedLerp" options:NSKeyValueObservingOptionNew context:NULL];
[[PRTween sharedInstance] performSelector:@selector(addTweenOperation:) withObject:operation afterDelay:0];
return operation;
}
#endif
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
PRTweenOperation *operation = (PRTweenOperation*)object;
if ([operation.period isKindOfClass:[PRTweenLerpPeriod class]]) {
PRTweenLerpPeriod *lerpPeriod = (PRTweenLerpPeriod*)operation.period;
NSUInteger bufferSize = 0;
NSGetSizeAndAlignment([lerpPeriod.tweenedLerp objCType], &bufferSize, NULL);
void *tweenedValue = malloc(bufferSize);
[lerpPeriod.tweenedLerp getValue:tweenedValue];
if (operation.boundObject && [operation.boundObject respondsToSelector:operation.boundGetter] && [operation.boundObject respondsToSelector:operation.boundSetter]) {
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[[operation.boundObject class] instanceMethodSignatureForSelector:operation.boundSetter]];
[invocation setTarget:operation.boundObject];
[invocation setSelector:operation.boundSetter];
[invocation setArgument:tweenedValue atIndex:2];
[invocation invoke];
}
free(tweenedValue);
} else {
CGFloat tweenedValue = operation.period.tweenedValue;
if (operation.boundObject && [operation.boundObject respondsToSelector:operation.boundGetter] && [operation.boundObject respondsToSelector:operation.boundSetter]) {
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[[operation.boundObject class] instanceMethodSignatureForSelector:operation.boundSetter]];
[invocation setTarget:operation.boundObject];
[invocation setSelector:operation.boundSetter];
[invocation setArgument:&tweenedValue atIndex:2];
[invocation invoke];
} else if (operation.boundRef) {
*operation.boundRef = tweenedValue;
}
}
}
- (id)init {
self = [super init];
if (self != nil) {
tweenOperations = [[NSMutableArray alloc] init];
expiredTweenOperations = [[NSMutableArray alloc] init];
timeOffset = 0;
if (timer == nil) {
timer = [NSTimer scheduledTimerWithTimeInterval:kPRTweenFramerate target:self selector:@selector(update) userInfo:nil repeats:YES];
}
self.defaultTimingFunction = &PRTweenTimingFunctionQuadInOut;
}
return self;
}
- (PRTweenOperation*)addTweenOperation:(PRTweenOperation*)operation {
if (useBuiltInAnimationsWhenPossible && !operation.override) {
if (animationSelectorsForCoreAnimation == nil) {
animationSelectorsForCoreAnimation = [[NSArray alloc] initWithObjects:
@"setBounds:", // CGRect
@"setPosition:", // CGPoint
@"setZPosition:", // CGFloat
@"setAnchorPoint:", // CGPoint
@"setAnchorPointZ:", // CGFloat
//@"setTransform:", // CATransform3D
//@"setSublayerTransform:", // CATransform3D
@"setFrame:", // CGRect
@"setContentsRect" // CGRect
@"setContentsScale:", // CGFloat
@"setContentsCenter:", // CGPoint
//@"setBackgroundColor:", // CGColorRef
@"setCornerRadius:", // CGFloat
@"setBorderWidth:", // CGFloat
@"setOpacity:", // CGFloat
//@"setShadowColor:", // CGColorRef
@"setShadowOpacity:", // CGFloat
@"setShadowOffset:", // CGSize
@"setShadowRadius:", // CGFloat
//@"setShadowPath:",
nil];
}
if (animationSelectorsForUIView == nil) {
animationSelectorsForUIView = [[NSArray alloc] initWithObjects:
@"setFrame:", // CGRect
@"setBounds:", // CGRect
@"setCenter:", // CGPoint
@"setTransform:", // CGAffineTransform
@"setAlpha:", // CGFloat
//@"setBackgroundColor:", // UIColor
@"setContentStretch:", // CGRect
nil];
}
if (operation.boundSetter && operation.boundObject && !(operation.timingFunction == &PRTweenTimingFunctionCADefault ||
operation.timingFunction == &PRTweenTimingFunctionCAEaseIn ||
operation.timingFunction == &PRTweenTimingFunctionCAEaseOut ||
operation.timingFunction == &PRTweenTimingFunctionCAEaseInOut ||
operation.timingFunction == &PRTweenTimingFunctionCALinear ||
operation.timingFunction == &PRTweenTimingFunctionUIViewEaseIn ||
operation.timingFunction == &PRTweenTimingFunctionUIViewEaseOut ||
operation.timingFunction == &PRTweenTimingFunctionUIViewEaseInOut ||
operation.timingFunction == &PRTweenTimingFunctionUIViewLinear ||
operation.timingFunction == NULL)) {
goto complete;
}
if (operation.boundSetter && operation.boundObject && [operation.boundObject isKindOfClass:[CALayer class]]) {
for (NSString *selector in animationSelectorsForCoreAnimation) {
NSString *setter = NSStringFromSelector(operation.boundSetter);
if ([selector isEqualToString:setter]) {
NSLog(@"Using Core Animation for %@", NSStringFromSelector(operation.boundSetter));
operation.canUseBuiltAnimation = YES;
NSString *propertyUnformatted = [selector stringByReplacingCharactersInRange:NSMakeRange(0, 3) withString:@""];
NSString *propertyFormatted = [[propertyUnformatted stringByReplacingCharactersInRange:NSMakeRange(0, 1) withString:[[propertyUnformatted substringToIndex:1] lowercaseString]] substringToIndex:[propertyUnformatted length] - 1];
//NSLog(@"%@", propertyFormatted);
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:propertyFormatted];
animation.duration = operation.period.duration;
if (![operation.period isKindOfClass:[PRTweenLerpPeriod class]] && ![operation.period conformsToProtocol:@protocol(PRTweenLerpPeriod)]) {
animation.fromValue = [NSNumber numberWithFloat:operation.period.startValue];
animation.toValue = [NSNumber numberWithFloat:operation.period.endValue];
} else {
PRTweenLerpPeriod *period = (PRTweenLerpPeriod*)operation.period;
animation.fromValue = period.startLerp;
animation.toValue = period.endLerp;
}
if (operation.timingFunction == &PRTweenTimingFunctionCAEaseIn) {
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
} else if (operation.timingFunction == &PRTweenTimingFunctionCAEaseOut) {
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
} else if (operation.timingFunction == &PRTweenTimingFunctionCAEaseInOut) {
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
} else if (operation.timingFunction == &PRTweenTimingFunctionCALinear) {
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
} else {
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault];
}
[operation.boundObject setValue:animation.toValue forKeyPath:propertyFormatted];
[operation.boundObject addAnimation:animation forKey:@"PRTweenCAAnimation"];
goto complete;
}
}
} else if (operation.boundSetter && operation.boundObject && [operation.boundObject isKindOfClass:[UIView class]]) {
for (NSString *selector in animationSelectorsForUIView) {
NSString *setter = NSStringFromSelector(operation.boundSetter);
if ([selector isEqualToString:setter]) {
NSLog(@"Using UIView Animation for %@", NSStringFromSelector(operation.boundSetter));
operation.canUseBuiltAnimation = YES;
NSString *propertyUnformatted = [selector stringByReplacingCharactersInRange:NSMakeRange(0, 3) withString:@""];
NSString *propertyFormatted = [[propertyUnformatted stringByReplacingCharactersInRange:NSMakeRange(0, 1) withString:[[propertyUnformatted substringToIndex:1] lowercaseString]] substringToIndex:[propertyUnformatted length] - 1];
NSValue *fromValue = nil;
NSValue *toValue = nil;
if (![operation.period isKindOfClass:[PRTweenLerpPeriod class]] && ![operation.period conformsToProtocol:@protocol(PRTweenLerpPeriod)]) {
fromValue = [NSNumber numberWithFloat:operation.period.startValue];
toValue = [NSNumber numberWithFloat:operation.period.endValue];
} else {
PRTweenLerpPeriod *period = (PRTweenLerpPeriod*)operation.period;
fromValue = period.startLerp;
toValue = period.endLerp;
}
[operation.boundObject setValue:fromValue forKeyPath:propertyFormatted];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:operation.period.duration];
if (operation.timingFunction == &PRTweenTimingFunctionUIViewEaseIn) {
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
} else if (operation.timingFunction == &PRTweenTimingFunctionUIViewEaseOut) {
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
} else if (operation.timingFunction == &PRTweenTimingFunctionUIViewEaseInOut) {
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
} else if (operation.timingFunction == &PRTweenTimingFunctionUIViewLinear) {
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
}
[operation.boundObject setValue:toValue forKeyPath:propertyFormatted];
[UIView commitAnimations];
goto complete;
}
}
}
}
complete:
[tweenOperations addObject:operation];
return operation;
}
#if NS_BLOCKS_AVAILABLE
- (PRTweenOperation*)addTweenPeriod:(PRTweenPeriod *)period
updateBlock:(void (^)(PRTweenPeriod *period))updateBlock
completionBlock:(void (^)())completeBlock {
return [self addTweenPeriod:period updateBlock:updateBlock completionBlock:completeBlock timingFunction:self.defaultTimingFunction];
}
- (PRTweenOperation*)addTweenPeriod:(PRTweenPeriod *)period
updateBlock:(void (^)(PRTweenPeriod *period))anUpdateBlock
completionBlock:(void (^)())completeBlock
timingFunction:(PRTweenTimingFunction)timingFunction {
PRTweenOperation *tweenOperation = [PRTweenOperation new];
tweenOperation.period = period;
tweenOperation.timingFunction = timingFunction;
tweenOperation.updateBlock = anUpdateBlock;
tweenOperation.completeBlock = completeBlock;
return [self addTweenOperation:tweenOperation];
}
#endif
- (PRTweenOperation*)addTweenPeriod:(PRTweenPeriod *)period target:(NSObject *)target selector:(SEL)selector {
return [self addTweenPeriod:period target:target selector:selector timingFunction:self.defaultTimingFunction];
}
- (PRTweenOperation*)addTweenPeriod:(PRTweenPeriod *)period target:(NSObject *)target selector:(SEL)selector timingFunction:(PRTweenTimingFunction)timingFunction {
PRTweenOperation *tweenOperation = [PRTweenOperation new];
tweenOperation.period = period;
tweenOperation.target = target;
tweenOperation.timingFunction = timingFunction;
tweenOperation.updateSelector = selector;
return [self addTweenOperation:tweenOperation];
}
- (void)removeTweenOperation:(PRTweenOperation *)tweenOperation {
if (tweenOperation != nil) {
if ([tweenOperations containsObject:tweenOperation]) {
[expiredTweenOperations addObject:tweenOperation];
}
}
}
+ (SEL)setterFromProperty:(NSString *)property {
return NSSelectorFromString([NSString stringWithFormat:@"set%@:", [property stringByReplacingCharactersInRange:NSMakeRange(0, 1) withString:[[property substringToIndex:1] capitalizedString]]]);
}
- (void)update {
timeOffset += kPRTweenFramerate;
for (PRTweenOperation *tweenOperation in tweenOperations) {
PRTweenPeriod *period = tweenOperation.period;
// if operation is delayed, pass over it for now
if (timeOffset <= period.startOffset + period.delay) {
continue;
}
CGFloat (*timingFunction)(CGFloat, CGFloat, CGFloat, CGFloat) = tweenOperation.timingFunction;
if (timingFunction == NULL) {
timingFunction = self.defaultTimingFunction;
}
if (timingFunction != NULL && tweenOperation.canUseBuiltAnimation == NO) {
if (timeOffset <= period.startOffset + period.delay + period.duration) {
if ([period isKindOfClass:[PRTweenLerpPeriod class]]) {
if ([period conformsToProtocol:@protocol(PRTweenLerpPeriod)]) {
PRTweenLerpPeriod <PRTweenLerpPeriod> *lerpPeriod = (PRTweenLerpPeriod <PRTweenLerpPeriod> *)period;
CGFloat progress = timingFunction(timeOffset - period.startOffset - period.delay, 0.0, 1.0, period.duration);
[lerpPeriod setProgress:progress];
} else {
// @TODO: Throw exception
NSLog(@"Class doesn't conform to PRTweenLerp");
}
} else {
// if tween operation is valid, calculate tweened value using timing function
period.tweenedValue = timingFunction(timeOffset - period.startOffset - period.delay, period.startValue, period.endValue - period.startValue, period.duration);
}
} else {
// move expired tween operations to list for cleanup
period.tweenedValue = period.endValue;
[expiredTweenOperations addObject:tweenOperation];
}
NSObject *target = tweenOperation.target;
SEL selector = tweenOperation.updateSelector;
if (period != nil) {
if (target != nil && selector != NULL) {
[target performSelector:selector withObject:period afterDelay:0];
}
// Check to see if blocks/GCD are supported
if (kCFCoreFoundationVersionNumber >= kCFCoreFoundationVersionNumber_iOS_4_0) {
// fire off update block
if (tweenOperation.updateBlock != NULL) {
tweenOperation.updateBlock(period);
}
}
}
} else if (tweenOperation.canUseBuiltAnimation == YES) {
if (timeOffset > period.startOffset + period.delay + period.duration) {
[expiredTweenOperations addObject:tweenOperation];
}
}
}
// clean up expired tween operations
for (__strong PRTweenOperation *tweenOperation in expiredTweenOperations) {
if (tweenOperation.completeSelector) [tweenOperation.target performSelector:tweenOperation.completeSelector withObject:nil afterDelay:0];
// Check to see if blocks/GCD are supported
if (kCFCoreFoundationVersionNumber >= kCFCoreFoundationVersionNumber_iOS_4_0) {
if (tweenOperation.completeBlock != NULL) {
tweenOperation.completeBlock();
}
}
// @HACK: Come up with a better pattern for removing observers.
@try {
[tweenOperation removeObserver:[PRTween sharedInstance] forKeyPath:@"period.tweenedValue"];
} @catch (id exception) {
}
@try {
[tweenOperation removeObserver:[PRTween sharedInstance] forKeyPath:@"period.tweenedLerp"];
} @catch (id exception) {
}
[tweenOperations removeObject:tweenOperation];
tweenOperation = nil;
}
[expiredTweenOperations removeAllObjects];
}
- (void)dealloc {
tweenOperations = nil;
expiredTweenOperations = nil;
[timer invalidate];
}
@end