-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTRPushBackController.m
More file actions
305 lines (239 loc) · 9.49 KB
/
TRPushBackController.m
File metadata and controls
305 lines (239 loc) · 9.49 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
//
// TRPushBackController.m
// TRPushBackControllerExample
//
// Created by Tom Redman on 2014-04-13.
// Copyright (c) 2014 Tom Redman. All rights reserved.
//
#import "TRPushBackController.h"
#import <QuartzCore/QuartzCore.h>
#define kTagSnapshot 1111
#define kStatusBarHeight 20.0f
typedef NS_ENUM(BOOL, TRPushDirection) {
TRPushDirectionPushBack = YES,
TRPushDirectionPullForward = NO
};
NSString * const kNotificationWillPushBack = @"kNotificationWillPushBack";
NSString * const kNotificationDidPushBack = @"kNotificationDidPushBack";
NSString * const kNotificationWillPullForward = @"kNotificationWillPullForward";
NSString * const kNotificationDidPullForward = @"kNotificationDidPullForward";
NSString *kAnimationIdentifierPushBack = @"pushBackAnimation";
NSString *kAnimationIdentifierPullForward = @"pullForwardAnimation";
@interface TRPushBackController ()
@property (nonatomic) BOOL useImageForStatusBar;
@property (nonatomic, strong) Completion pushBackCompletion;
@property (nonatomic, strong) Completion pullForwardCompletion;
@end
@implementation TRPushBackController
#pragma mark - Convenience initializer
+ (instancetype)controller
{
TRPushBackController *controller = [[TRPushBackController alloc] initWithNibName:@"TRPushBackController" bundle:nil];
return controller;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.layer.zPosition = -4000;
}
- (IBAction)pushBackButtonTapped:(id)sender
{
[self togglePush];
}
- (BOOL)prefersStatusBarHidden
{
if (_useImageForStatusBar) {
return YES;
}
if (self.rootViewController) {
if ([self.rootViewController isKindOfClass:[UINavigationController class]]) {
return [((UINavigationController *)self.rootViewController).topViewController prefersStatusBarHidden];
}
return [self.rootViewController prefersStatusBarHidden];
}
return NO;
}
- (UIStatusBarStyle)preferredStatusBarStyle
{
if (self.rootViewController) {
if ([self.rootViewController isKindOfClass:[UINavigationController class]]) {
return [((UINavigationController *)self.rootViewController).topViewController preferredStatusBarStyle];
}
return [self.rootViewController preferredStatusBarStyle];
}
return UIStatusBarStyleDefault;
}
#pragma mark - Push back methods
- (void)togglePush
{
if (self.isPushedBack) {
[self pullForward];
}
else {
[self pushBack];
}
}
- (void)pushBackWithCompletion:(Completion)completion
{
self.pushBackCompletion = completion;
[self pushBack];
}
- (void)pullForwardWithCompletion:(Completion)completion
{
self.pullForwardCompletion = completion;
[self pullForward];
}
- (void)pushBack
{
if (!self.isPushedBack) {
[[NSNotificationCenter defaultCenter] postNotificationName:kNotificationWillPushBack object:nil];
self.isPushedBack = YES;
self.view.userInteractionEnabled = NO;
[self useImageForStatusBar:YES];
[self.view.layer addAnimation:[self pushAnimationGroup:TRPushDirectionPushBack] forKey:kAnimationIdentifierPushBack];
}
}
- (void)pullForward
{
if (self.isPushedBack) {
[[NSNotificationCenter defaultCenter] postNotificationName:kNotificationWillPullForward object:nil];
[self.view.layer addAnimation:[self pushAnimationGroup:TRPushDirectionPullForward] forKey:kAnimationIdentifierPullForward];
}
}
- (void)setRootViewController:(UIViewController *)viewController
{
if (_rootViewController && [[self childViewControllers] containsObject:_rootViewController]) {
[_rootViewController willMoveToParentViewController:nil];
[_rootViewController removeFromParentViewController];
}
_rootViewController = viewController;
viewController.view.frame = self.view.bounds;
[self.view addSubview:viewController.view];
[self addChildViewController:_rootViewController];
[_rootViewController didMoveToParentViewController:self];
}
- (void)setLeftViewController:(UIViewController *)viewController
{
_leftViewController = viewController;
}
- (void)setRightViewController:(UIViewController *)viewController
{
_rightViewController = viewController;
}
- (void)slide:(TRPushBackSliderDirection)direction
{
}
#pragma mark - Private methods
#define kAnimationDuration 0.6
-(CAAnimationGroup *)pushAnimationGroup:(TRPushDirection)pushDirection
{
CATransform3D t1 = CATransform3DIdentity;
CATransform3D t2 = CATransform3DIdentity;
/**
* Set m34 to allow for a perspective rotation
* http://stackoverflow.com/questions/10913676/why-does-a-calayers-transform3ds-m34-need-to-be-modified-before-applying-the-r
*/
t1.m34 = 1.0/-1000;
t2.m34 = t1.m34;
/**
* Rotate the top back 35 degrees
*/
t1 = CATransform3DRotate(t1, 35.0f * M_PI/180.0f, 1, 0, 0);
/**
* Simply scale down to complete the effect
*/
t2 = CATransform3DScale(t2, 0.9, 0.9, 1);
/**
* Setup the animations using the transforms
*/
CABasicAnimation *firstHalfAnimation = [CABasicAnimation animationWithKeyPath:@"transform"];
firstHalfAnimation.toValue = [NSValue valueWithCATransform3D:t1];
firstHalfAnimation.duration = kAnimationDuration/2;
firstHalfAnimation.fillMode = kCAFillModeForwards;
firstHalfAnimation.removedOnCompletion = NO;
[firstHalfAnimation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
NSValue *secondHalfToValue = [NSValue valueWithCATransform3D:((pushDirection == TRPushDirectionPushBack) ? t2 : CATransform3DIdentity)];
CABasicAnimation *secondHalfAnimation = [CABasicAnimation animationWithKeyPath:@"transform"];
secondHalfAnimation.toValue = secondHalfToValue;
secondHalfAnimation.beginTime = firstHalfAnimation.duration/2;
secondHalfAnimation.duration = firstHalfAnimation.duration;
secondHalfAnimation.fillMode = kCAFillModeForwards;
secondHalfAnimation.removedOnCompletion = NO;
[secondHalfAnimation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
NSArray *animations = @[firstHalfAnimation,secondHalfAnimation];
CAAnimationGroup *animationGroup = [CAAnimationGroup animation];
animationGroup.fillMode = kCAFillModeForwards;
animationGroup.removedOnCompletion = NO;
animationGroup.duration = secondHalfAnimation.beginTime + secondHalfAnimation.duration;
animationGroup.animations = animations;
animationGroup.delegate = self;
return animationGroup;
}
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
if (flag) {
self.view.userInteractionEnabled = YES;
if (anim == [[self.view layer] animationForKey:kAnimationIdentifierPullForward]) {
[self pullForwardDidComplete];
}
else {
[self pushBackDidComplete];
}
}
}
- (void)pushBackDidComplete
{
if (self.pushBackCompletion) {
self.pushBackCompletion();
self.pushBackCompletion = nil;
}
[[NSNotificationCenter defaultCenter] postNotificationName:kNotificationDidPushBack object:nil];
}
- (void)pullForwardDidComplete
{
self.isPushedBack = NO;
[self useImageForStatusBar:NO];
if (self.pullForwardCompletion) {
self.pullForwardCompletion();
self.pullForwardCompletion = nil;
}
[[NSNotificationCenter defaultCenter] postNotificationName:kNotificationDidPullForward object:nil];
}
- (void)useImageForStatusBar:(BOOL)useImageForStatusBar
{
self.useImageForStatusBar = useImageForStatusBar;
if (self.useImageForStatusBar) {
// Remove stale status bar image if it exists
[[self.view viewWithTag:kTagSnapshot] removeFromSuperview];
UIView *snapshot = [[UIScreen mainScreen] snapshotViewAfterScreenUpdates:YES];
[snapshot setTag:kTagSnapshot];
for (UIView *subview in [snapshot subviews]) {
subview.contentMode = UIViewContentModeTop;
}
//crop to just the status bar
snapshot.clipsToBounds = YES;
snapshot.frame = CGRectMake(0, 0, snapshot.bounds.size.width, kStatusBarHeight);
[self.view addSubview:snapshot];
/**
* Sneaky workaround to keep navigationBars from shifting up when hiding
* the statusBar. This must happen before the status bar update.
*/
self.rootViewController.view.frame = CGRectMake(0, 1, self.rootViewController.view.frame.size.width, self.rootViewController.view.frame.size.height);
if ([self respondsToSelector:@selector(setNeedsStatusBarAppearanceUpdate)]) {
[self setNeedsStatusBarAppearanceUpdate];
}
}
else {
/**
* Sneaky workaround to keep navigationBars from shifting up when hiding
* the statusBar. This must happen before the status bar update.
*/
self.rootViewController.view.frame = CGRectMake(0, 0, self.rootViewController.view.frame.size.width, self.rootViewController.view.frame.size.height);
if ([self respondsToSelector:@selector(setNeedsStatusBarAppearanceUpdate)]) {
[self setNeedsStatusBarAppearanceUpdate];
}
[[self.view viewWithTag:kTagSnapshot] setAlpha:0.0];
[[self.view viewWithTag:kTagSnapshot] removeFromSuperview];
}
}
@end