-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAnimationDelegate.m
More file actions
583 lines (483 loc) · 20.2 KB
/
Copy pathAnimationDelegate.m
File metadata and controls
583 lines (483 loc) · 20.2 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
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
/*
File: AnimationDelegate.m
Abstract: Animation Delegate is the helper to handle callbacks
from transform operations. The animation
delegate should have knowledge of how and what kind of transform
should be applied to current animation frame, based on the type
of animation and various user settings.
Copyright (c) 2011 Dillion Tan
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 "AnimationDelegate.h"
#import <QuartzCore/QuartzCore.h>
#import "GenericAnimationView.h"
#import "AnimationFrame.h"
@implementation AnimationDelegate
@synthesize transformView;
@synthesize controller;
@synthesize nextDuration;
@synthesize sequenceType;
@synthesize animationState;
@synthesize animationLock;
@synthesize shadow;
@synthesize repeatDelay;
@synthesize repeat;
@synthesize sensitivity;
@synthesize gravity;
@synthesize perspectiveDepth;
- (id)initWithSequenceType:(SequenceType)aType
directionType:(DirectionType)aDirection
{
if ((self = [super init])) {
transformView = nil;
controller = nil;
sequenceType = aType;
currentDirection = aDirection;
repeat = NO;
// default values
nextDuration = 0.6;
repeatDelay = 0.2;
sensitivity = 40;
gravity = 2;
perspectiveDepth = 500;
shadow = YES;
if (sequenceType == kSequenceAuto) {
repeat = YES;
} else {
repeat = NO;
}
}
return self;
}
- (BOOL)startAnimation:(DirectionType)aDirection
{
if (animationState == 0) {
[NSObject cancelPreviousPerformRequestsWithTarget:self];
if (aDirection != kDirectionNone) {
currentDirection = aDirection;
}
switch (currentDirection) {
case kDirectionForward:
[self setTransformValue:10.0f delegating:YES];
return YES;
break;
case kDirectionBackward:
[self setTransformValue:-10.0f delegating:YES];
return YES;
break;
default:break;
}
}
return NO;
}
- (void)animationDidStop:(CABasicAnimation *)theAnimation finished:(BOOL)flag
{
if (flag) {
switch (animationState) {
case 0:
break;
case 1: {
switch (transformView.animationType) {
case kAnimationFlipVertical:
case kAnimationFlipHorizontal: {
[self animationCallback];
}
break;
default:
break;
}
}
break;
default:
break;
}
}
}
- (void)animationCallback
{
[self resetTransformValues];
if (repeat && sequenceType == kSequenceAuto) {
// the recommended way to queue CAAnimations by Apple is to offset the beginTime,
// but doing so requires changing the fillmode to kCAFillModeBackwards
// using perform selector allows maintaining the fillmode value of the original animation
[self performSelector:@selector(startAnimation:) withObject:nil afterDelay:repeatDelay];
}
}
- (void)endStateWithSpeed:(float)aVelocity
{
if (value == 0.0f) {
[self resetTransformValues];
} else if (value == 10.0f) {
[self resetTransformValues];
} else {
AnimationFrame* currentFrame = [transformView.imageStackArray lastObject];
CALayer *targetLayer;
int aX, aY, aZ;
int rotationModifier;
switch (transformView.animationType) {
case kAnimationFlipVertical:
aX = 1;
aY = 0;
aZ = 0;
rotationModifier = -1;
break;
case kAnimationFlipHorizontal:
aX = 0;
aY = 1;
aZ = 0;
rotationModifier = 1;
break;
default:break;
}
float rotationAfterDirection;
if (currentDirection == kDirectionForward) {
rotationAfterDirection = M_PI * rotationModifier;
targetLayer = [currentFrame.animationImages lastObject];
} else if (currentDirection == kDirectionBackward) {
rotationAfterDirection = -M_PI * rotationModifier;
targetLayer = [currentFrame.animationImages objectAtIndex:0];
}
CALayer *targetShadowLayer;
CATransform3D aTransform = CATransform3DIdentity;
aTransform.m34 = 1.0 / -perspectiveDepth;
[targetLayer setValue:[NSValue valueWithCATransform3D:CATransform3DRotate(aTransform,rotationAfterDirection/10.0 * value, aX, aY, aZ)] forKeyPath:@"transform"];
for (CALayer *layer in targetLayer.sublayers) {
[layer removeAllAnimations];
}
[targetLayer removeAllAnimations];
if (gravity > 0) {
animationState = 1;
if (value+aVelocity <= 5.0f) {
targetShadowLayer = [targetLayer.sublayers objectAtIndex:1];
[self setTransformProgress:rotationAfterDirection / 10.0 * value
:0.0f
:1.0f/(gravity+aVelocity)
:aX :aY :aZ
:YES
:NO
:kCAFillModeForwards
:targetLayer];
if (shadow) {
[self setOpacityProgress:oldOpacityValue
:0.0f
:0.0f
:currentDuration
:kCAFillModeForwards
:targetShadowLayer];
}
value = 0.0f;
} else {
targetShadowLayer = [targetLayer.sublayers objectAtIndex:3];
[self setTransformProgress:rotationAfterDirection / 10.0 * value
:rotationAfterDirection
:1.0f/(gravity+aVelocity)
:aX :aY :aZ
:YES
:NO
:kCAFillModeForwards
:targetLayer];
if (shadow) {
[self setOpacityProgress:oldOpacityValue
:0.0f
:0.0f
:currentDuration
:kCAFillModeForwards
:targetShadowLayer];
}
value = 10.0f;
}
}
}
}
- (void)resetTransformValues
{
AnimationFrame* currentFrame = [transformView.imageStackArray lastObject];
CALayer *targetLayer;
CALayer *targetShadowLayer, *targetShadowLayer2;
if (currentDirection == kDirectionForward) {
targetLayer = [currentFrame.animationImages lastObject];
} else if (currentDirection == kDirectionBackward) {
targetLayer = [currentFrame.animationImages objectAtIndex:0];
}
targetShadowLayer = [targetLayer.sublayers objectAtIndex:1];
targetShadowLayer2 = [targetLayer.sublayers objectAtIndex:3];
[CATransaction begin];
[CATransaction setDisableActions:YES];
[targetLayer setValue:[NSValue valueWithCATransform3D:CATransform3DIdentity] forKeyPath:@"transform"];
targetShadowLayer.opacity = 0.0f;
targetShadowLayer2.opacity = 0.0f;
for (CALayer *layer in targetLayer.sublayers) {
[layer removeAllAnimations];
}
[targetLayer removeAllAnimations];
targetLayer.zPosition = 0;
CATransform3D aTransform = CATransform3DIdentity;
targetLayer.sublayerTransform = aTransform;
if (value == 10.0f) {
[transformView rearrangeLayers:currentDirection :3];
} else {
[transformView rearrangeLayers:currentDirection :2];
}
[CATransaction commit];
if (controller && [controller respondsToSelector:@selector(animationDidFinish:)]) {
if (currentDirection == kDirectionForward && value == 10.0f) {
[controller animationDidFinish:1];
} else if (currentDirection == kDirectionBackward && value == 10.0f) {
[controller animationDidFinish:-1];
}
}
animationState = 0;
animationLock = NO;
transitionImageBackup = nil;
value = 0.0f;
oldOpacityValue = 0.0f;
}
// set the progress of the animation
- (void)setTransformValue:(float)aValue delegating:(BOOL)bValue
{
currentDuration = nextDuration;
int frameCount = [transformView.imageStackArray count];
AnimationFrame* currentFrame = [transformView.imageStackArray lastObject];
CALayer *targetLayer;
AnimationFrame* nextFrame = [transformView.imageStackArray objectAtIndex:frameCount-2];
AnimationFrame* previousFrame = [transformView.imageStackArray objectAtIndex:0];
int aX, aY, aZ;
int rotationModifier;
switch (transformView.animationType) {
case kAnimationFlipVertical:
aX = 1;
aY = 0;
aZ = 0;
rotationModifier = -1;
break;
case kAnimationFlipHorizontal:
aX = 0;
aY = 1;
aZ = 0;
rotationModifier = 1;
break;
default:break;
}
float rotationAfterDirection;
if (transitionImageBackup == nil) {
if (aValue - value >= 0.0f) {
currentDirection = kDirectionForward;
switch (transformView.animationType) {
case kAnimationFlipVertical:
case kAnimationFlipHorizontal: {
targetLayer = [currentFrame.animationImages lastObject];
targetLayer.zPosition = 100;
}
break;
default:
break;
}
animationState++;
} else if (aValue - value < 0.0f) {
currentDirection = kDirectionBackward;
[transformView rearrangeLayers:currentDirection :1];
switch (transformView.animationType) {
case kAnimationFlipVertical:
case kAnimationFlipHorizontal: {
targetLayer = [currentFrame.animationImages objectAtIndex:0];
targetLayer.zPosition = 100;
}
break;
default:
break;
}
animationState++;
}
}
if (currentDirection == kDirectionForward) {
rotationAfterDirection = M_PI * rotationModifier;
targetLayer = [currentFrame.animationImages lastObject];
} else if (currentDirection == kDirectionBackward) {
rotationAfterDirection = -M_PI * rotationModifier;
targetLayer = [currentFrame.animationImages objectAtIndex:0];
}
float adjustedValue;
float opacityValue;
if (sequenceType == kSequenceControlled) {
adjustedValue = fabs(aValue * (sensitivity/1000.0));
} else {
adjustedValue = fabs(aValue);
}
adjustedValue = MAX(0.0, adjustedValue);
adjustedValue = MIN(10.0, adjustedValue);
if (adjustedValue <= 5.0f) {
opacityValue = adjustedValue/10.0f;
} else if (adjustedValue > 5.0f) {
opacityValue = (10.0f - adjustedValue)/10.0f;
}
CALayer *targetFrontLayer, *targetBackLayer = nil;
CALayer *targetShadowLayer, *targetShadowLayer2 = nil;
switch (transformView.animationType) {
case kAnimationFlipVertical: {
switch (currentDirection) {
case kDirectionForward: {
targetFrontLayer = [targetLayer.sublayers objectAtIndex:2];
CALayer *nextLayer = [nextFrame.animationImages objectAtIndex:0];
targetBackLayer = [nextLayer.sublayers objectAtIndex:0];
}
break;
case kDirectionBackward: {
targetFrontLayer = [targetLayer.sublayers objectAtIndex:2];
CALayer *previousLayer = [previousFrame.animationImages objectAtIndex:1];
targetBackLayer = [previousLayer.sublayers objectAtIndex:0];
}
break;
default:
break;
}
}
break;
case kAnimationFlipHorizontal: {
switch (currentDirection) {
case kDirectionForward: {
targetFrontLayer = [targetLayer.sublayers objectAtIndex:2];
CALayer *nextLayer = [nextFrame.animationImages objectAtIndex:0];
targetBackLayer = [nextLayer.sublayers objectAtIndex:0];
}
break;
case kDirectionBackward: {
targetFrontLayer = [targetLayer.sublayers objectAtIndex:2];
CALayer *previousLayer = [previousFrame.animationImages objectAtIndex:1];
targetBackLayer = [previousLayer.sublayers objectAtIndex:0];
}
break;
default:
break;
}
}
break;
default:break;
}
if (adjustedValue == 10.0f && value == 0.0f) {
targetShadowLayer = [targetLayer.sublayers objectAtIndex:1];
targetShadowLayer2 = [targetLayer.sublayers objectAtIndex:3];
}
else if (adjustedValue <= 5.0f) {
targetShadowLayer = [targetLayer.sublayers objectAtIndex:1];
}
else {
targetShadowLayer = [targetLayer.sublayers objectAtIndex:3];
}
[CATransaction begin];
[CATransaction setDisableActions:YES];
CATransform3D aTransform = CATransform3DIdentity;
aTransform.m34 = 1.0 / -perspectiveDepth;
[targetLayer setValue:[NSValue valueWithCATransform3D:CATransform3DRotate(aTransform, rotationAfterDirection/10.0 * value, aX, aY, aZ)] forKeyPath:@"transform"];
targetShadowLayer.opacity = oldOpacityValue;
if (targetShadowLayer2) targetShadowLayer2.opacity = oldOpacityValue;
for (CALayer *layer in targetLayer.sublayers) {
[layer removeAllAnimations];
}
[targetLayer removeAllAnimations];
[CATransaction commit];
if (adjustedValue != value) {
CATransform3D aTransform = CATransform3DIdentity;
aTransform.m34 = 1.0 / -perspectiveDepth;
targetLayer.sublayerTransform = aTransform;
if (transitionImageBackup == nil) { //transition has begun, copy the layer content for the reverse layer
CGImageRef tempImageRef = (CGImageRef)targetBackLayer.contents;
//NSLog(@"%s:%d imageref=%@", __func__, __LINE__, tempImageRef);
transitionImageBackup = (CGImageRef)targetFrontLayer.contents;
targetFrontLayer.contents = (id)tempImageRef;
}
[self setTransformProgress:(rotationAfterDirection/10.0 * value)
:(rotationAfterDirection/10.0 * adjustedValue)
:currentDuration
:aX :aY :aZ
:bValue
:NO
:kCAFillModeForwards
:targetLayer];
if (shadow) {
if (oldOpacityValue == 0.0f && oldOpacityValue == opacityValue) {
[self setOpacityProgress:0.0f
:0.5f
:0.0f
:currentDuration/2
:kCAFillModeForwards
:targetShadowLayer];
[self setOpacityProgress:0.5f
:0.0f
:currentDuration/2
:currentDuration/2
:kCAFillModeBackwards
:targetShadowLayer2];
} else {
[self setOpacityProgress:oldOpacityValue
:opacityValue
:0.0f
:currentDuration
:kCAFillModeForwards
:targetShadowLayer];
}
}
value = adjustedValue;
oldOpacityValue = opacityValue;
}
}
- (void)setTransformProgress:(float)startTransformValue
:(float)endTransformValue
:(float)duration
:(int)aX
:(int)aY
:(int)aZ
:(BOOL)setDelegate
:(BOOL)removedOnCompletion
:(NSString *)fillMode
:(CALayer *)targetLayer
{
//NSLog(@"transform value %f, %f", startTransformValue, endTransformValue);
CATransform3D aTransform = CATransform3DIdentity;
aTransform.m34 = 1.0 / -perspectiveDepth;
CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"transform"];
anim.duration = duration;
anim.fromValue= [NSValue valueWithCATransform3D:CATransform3DRotate(aTransform, startTransformValue, aX, aY, aZ)];
anim.toValue=[NSValue valueWithCATransform3D:CATransform3DRotate(aTransform, endTransformValue, aX, aY, aZ)];
if (setDelegate) {
anim.delegate = self;
}
anim.removedOnCompletion = removedOnCompletion;
[anim setFillMode:fillMode];
[targetLayer addAnimation:anim forKey:@"transformAnimation"];
}
- (void)setOpacityProgress:(float)startOpacityValue
:(float)endOpacityValue
:(float)beginTime
:(float)duration
:(NSString *)fillMode
:(CALayer *)targetLayer
{
//NSLog(@"opacity value %f, %f, %@", startOpacityValue, endOpacityValue, targetLayer);
CFTimeInterval localMediaTime = [targetLayer convertTime:CACurrentMediaTime() fromLayer:nil];
CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"opacity"];
anim.duration = duration;
anim.fromValue= [NSNumber numberWithFloat:startOpacityValue];
anim.toValue= [NSNumber numberWithFloat:endOpacityValue];
anim.beginTime = localMediaTime+beginTime;
anim.removedOnCompletion = NO;
[anim setFillMode:fillMode];
[targetLayer addAnimation:anim forKey:@"opacityAnimation"];
}
@end