forked from ColinEberhardt/VCTransitionsLibrary
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCECardsAnimationController.m
More file actions
129 lines (96 loc) · 4.93 KB
/
CECardsAnimationController.m
File metadata and controls
129 lines (96 loc) · 4.93 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
//
// CEZoomAnimationController.m
// TransitionsDemo
//
// Created by Colin Eberhardt on 22/09/2013.
// Copyright (c) 2013 Colin Eberhardt. All rights reserved.
//
#import "CECardsAnimationController.h"
@implementation CECardsAnimationController
- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext fromVC:(UIViewController *)fromVC toVC:(UIViewController *)toVC fromView:(UIView *)fromView toView:(UIView *)toView {
if(self.reverse){
[self executeReverseAnimation:transitionContext fromVC:fromVC toVC:toVC fromView:fromView toView:toView];
} else {
[self executeForwardsAnimation:transitionContext fromVC:fromVC toVC:toVC fromView:fromView toView:toView];
}
}
-(void)executeForwardsAnimation:(id<UIViewControllerContextTransitioning>)transitionContext fromVC:(UIViewController *)fromVC toVC:(UIViewController *)toVC fromView:(UIView *)fromView toView:(UIView *)toView {
UIView* containerView = [transitionContext containerView];
// positions the to- view off the bottom of the sceen
CGRect frame = [transitionContext initialFrameForViewController:fromVC];
CGRect offScreenFrame = frame;
offScreenFrame.origin.y = offScreenFrame.size.height;
toView.frame = offScreenFrame;
[containerView insertSubview:toView aboveSubview:fromView];
CATransform3D t1 = [self firstTransform];
CATransform3D t2 = [self secondTransformWithView:fromView];
[UIView animateKeyframesWithDuration:self.duration delay:0.0 options:UIViewKeyframeAnimationOptionCalculationModeCubic animations:^{
// push the from- view to the back
[UIView addKeyframeWithRelativeStartTime:0.0f relativeDuration:0.4f animations:^{
fromView.layer.transform = t1;
fromView.alpha = 0.6;
}];
[UIView addKeyframeWithRelativeStartTime:0.2f relativeDuration:0.4f animations:^{
fromView.layer.transform = t2;
}];
// slide the to- view upwards. In his original implementation Tope used a 'spring' animation, however
// this does not work with keyframes, so we siulate it by overshooting the final location in
// the first keyframe
[UIView addKeyframeWithRelativeStartTime:0.6f relativeDuration:0.2f animations:^{
toView.frame = CGRectOffset(toView.frame, 0.0, -30.0);
}];
[UIView addKeyframeWithRelativeStartTime:0.8f relativeDuration:0.2f animations:^{
toView.frame = frame;
}];
} completion:^(BOOL finished) {
[transitionContext completeTransition:![transitionContext transitionWasCancelled]];
}];
}
-(void)executeReverseAnimation:(id<UIViewControllerContextTransitioning>)transitionContext fromVC:(UIViewController *)fromVC toVC:(UIViewController *)toVC fromView:(UIView *)fromView toView:(UIView *)toView {
UIView* containerView = [transitionContext containerView];
// positions the to- view behind the from- view
CGRect frame = [transitionContext initialFrameForViewController:fromVC];
toView.frame = frame;
CATransform3D scale = CATransform3DIdentity;
toView.layer.transform = CATransform3DScale(scale, 0.6, 0.6, 1);
toView.alpha = 0.6;
[containerView insertSubview:toView belowSubview:fromView];
CGRect frameOffScreen = frame;
frameOffScreen.origin.y = frame.size.height;
CATransform3D t1 = [self firstTransform];
[UIView animateKeyframesWithDuration:self.duration delay:0 options:UIViewKeyframeAnimationOptionCalculationModeCubic animations:^{
// push the from- view off the bottom of the screen
[UIView addKeyframeWithRelativeStartTime:0.0f relativeDuration:0.5f animations:^{
fromView.frame = frameOffScreen;
}];
// animate the to- view into place
[UIView addKeyframeWithRelativeStartTime:0.35f relativeDuration:0.35f animations:^{
toView.layer.transform = t1;
toView.alpha = 1.0;
}];
[UIView addKeyframeWithRelativeStartTime:0.75f relativeDuration:0.25f animations:^{
toView.layer.transform = CATransform3DIdentity;
}];
} completion:^(BOOL finished) {
if ([transitionContext transitionWasCancelled]) {
toView.layer.transform = CATransform3DIdentity;
toView.alpha = 1.0;
}
[transitionContext completeTransition:![transitionContext transitionWasCancelled]];
}];
}
-(CATransform3D)firstTransform{
CATransform3D t1 = CATransform3DIdentity;
t1.m34 = 1.0/-900;
t1 = CATransform3DScale(t1, 0.95, 0.95, 1);
t1 = CATransform3DRotate(t1, 15.0f * M_PI/180.0f, 1, 0, 0);
return t1;
}
-(CATransform3D)secondTransformWithView:(UIView*)view{
CATransform3D t2 = CATransform3DIdentity;
t2.m34 = [self firstTransform].m34;
t2 = CATransform3DTranslate(t2, 0, view.frame.size.height*-0.08, 0);
t2 = CATransform3DScale(t2, 0.8, 0.8, 1);
return t2;
}
@end