forked from coolstar/displaycandy
-
Notifications
You must be signed in to change notification settings - Fork 1
/
DCTransitionController.m
171 lines (141 loc) · 4.96 KB
/
DCTransitionController.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
#import "headers.h"
#import "DCTypes.h"
#import "DCTransitionController.h"
#import "DCBuiltInTransitionView.h"
#import "DCCoverTransitionView.h"
#import "DCRevealTransitionView.h"
#import "DCPushTransitionView.h"
#import "DCTVTubeTransitionView.h"
#import "DCSwingTransitionView.h"
#import "DCZoomTransitionView.h"
#import "DCSettings.h"
#include "DCFunctions.h"
@interface DCTransitionController ()
- (CGRect)transitionViewFrame;
- (CGAffineTransform)transitionViewTransform;
- (Class)classForCustomTransition:(NSInteger)transition;
- (BOOL)transitionIsBuiltIn:(NSString *)transition;
- (NSString *)transitionTypeForValue:(NSInteger)value;
@end
@implementation DCTransitionController
- (instancetype)init {
self = [super init];
if (self) {
// Set up the container view.
_view = [[UIView alloc] initWithFrame:[self transitionViewFrame]];
[_view setBackgroundColor:[UIColor blackColor]];
[_view setOpaque:YES];
}
return self;
}
- (void)beginTransition:(NSInteger)transition {
// Rotate the container view depending on the orientation.
CGAffineTransform rotationTransform = [self transitionViewTransform];
[[self view] setTransform:rotationTransform];
[[self view] setCenter:[[[self view] superview] center]];
// Set the animation type.
NSString *animationType = [self transitionTypeForValue:transition];
if ([self transitionIsBuiltIn:animationType]) {
_transitionView = [[DCBuiltInTransitionView alloc] initWithFrame:[[self view] bounds]];
[(DCBuiltInTransitionView *)_transitionView setType:animationType];
} else {
Class TransitionViewClass = [self classForCustomTransition:transition];
_transitionView = [[TransitionViewClass alloc] initWithFrame:[[self view] bounds]];
}
// Set the animation direction.
DCTransitionDirection direction = [[DCSettings sharedSettings] directionForMode:[self mode]];
// More setup.
[_transitionView setOpaque:YES];
[_transitionView setFromView:[self fromView]];
[_transitionView setToView:[self toView]];
[_transitionView setDelegate:self];
[_transitionView setApplicationIdentifier:[[self application] bundleIdentifier]];
[_transitionView setDirection:direction];
[_transitionView setMode:[self mode]];
[[self view] addSubview:_transitionView];
// Flush any changes to the view hierarchy
[CATransaction flush];
// Start the animation
CGFloat duration = [[DCSettings sharedSettings] durationForMode:[self mode]];
[_transitionView animateWithDuration:duration];
}
- (void)endTransition {
[_transitionView removeFromSuperview];
[[_transitionView layer] removeAllAnimations];
for (UIView *subview in [_transitionView subviews]) {
[[subview layer] removeAllAnimations];
}
}
- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag {
[[self delegate] displayCandyAnimationFinishedWithMode:[self mode] app:[self application]];
}
- (CGRect)transitionViewFrame {
CGRect screenBounds = [UIScreen mainScreen].bounds;
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] activeInterfaceOrientation];
if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight) {
return CGRectMake(screenBounds.origin.x, screenBounds.origin.y, screenBounds.size.height, screenBounds.size.width);
} else {
return screenBounds;
}
}
- (CGAffineTransform)transitionViewTransform {
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] activeInterfaceOrientation];
switch (orientation) {
case UIInterfaceOrientationPortrait:
return CGAffineTransformIdentity;
case UIInterfaceOrientationPortraitUpsideDown:
return CGAffineTransformMakeRotation(M_PI);
case UIInterfaceOrientationLandscapeLeft:
return CGAffineTransformMakeRotation(-M_PI_2);
case UIInterfaceOrientationLandscapeRight:
return CGAffineTransformMakeRotation(M_PI_2);
default:
return CGAffineTransformIdentity;
}
}
- (Class)classForCustomTransition:(NSInteger)transition {
switch (transition) {
case DCTransitionCover:
return [DCCoverTransitionView class];
case DCTransitionReveal:
return [DCRevealTransitionView class];
case DCTransitionPush:
return [DCPushTransitionView class];
case DCTransitionTVTube:
return [DCTVTubeTransitionView class];
case DCTransitionSwing:
return [DCSwingTransitionView class];
case DCTransitionZoomFromIcon:
return [DCZoomTransitionView class];
default:
return nil;
}
}
- (BOOL)transitionIsBuiltIn:(NSString *)transition {
NSArray *builtInTransitions = @[@"cube", @"oglFlip", @"pageCurl", @"pageUnCurl", @"rippleEffect", @"suckEffect", @"fade", @"cameraIris"];
BOOL isBuiltIn = [builtInTransitions containsObject:transition]
return isBuiltIn;
}
- (NSString *)transitionTypeForValue:(NSInteger)value {
switch (value) {
case 1:
return @"cube";
case 2:
return @"oglFlip";
case 3:
return @"pageCurl";
case 4:
return @"pageUnCurl";
case 5:
return @"rippleEffect";
case 6:
return @"suckEffect";
case 8:
return @"fade";
case 12:
return @"cameraIris";
default:
return nil;
}
}
@end