This repository was archived by the owner on Apr 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 163
/
Copy pathFlipBoardNavigationController.m
342 lines (285 loc) · 12.3 KB
/
FlipBoardNavigationController.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
//
// FlipBoardNavigationController.m
// iamkel.net
//
// Created by Michael henry Pantaleon on 4/30/13.
// Copyright (c) 2013 Michael Henry Pantaleon. All rights reserved.
//
#import "FlipBoardNavigationController.h"
#import <QuartzCore/QuartzCore.h>
static const CGFloat kAnimationDuration = 0.5f;
static const CGFloat kAnimationDelay = 0.0f;
static const CGFloat kMaxBlackMaskAlpha = 0.8f;
typedef enum {
PanDirectionNone = 0,
PanDirectionLeft = 1,
PanDirectionRight = 2
} PanDirection;
@interface FlipBoardNavigationController ()<UIGestureRecognizerDelegate>{
NSMutableArray *_gestures;
UIView *_blackMask;
CGPoint _panOrigin;
BOOL _animationInProgress;
CGFloat _percentageOffsetFromLeft;
}
- (void) addPanGestureToView:(UIView*)view;
- (void) rollBackViewController;
- (UIViewController *)currentViewController;
- (UIViewController *)previousViewController;
- (void) transformAtPercentage:(CGFloat)percentage ;
- (void) completeSlidingAnimationWithDirection:(PanDirection)direction;
- (void) completeSlidingAnimationWithOffset:(CGFloat)offset;
- (CGRect) getSlidingRectWithPercentageOffset:(CGFloat)percentage orientation:(UIInterfaceOrientation)orientation ;
- (CGRect) viewBoundsWithOrientation:(UIInterfaceOrientation)orientation;
@end
@implementation FlipBoardNavigationController
- (id) initWithRootViewController:(UIViewController*)rootViewController {
if (self = [super init]) {
self.viewControllers = [NSMutableArray arrayWithObject:rootViewController];
}
return self;
}
- (void) dealloc {
self.viewControllers = nil;
_gestures = nil;
_blackMask = nil;
}
#pragma mark - Load View
- (void) loadView {
[super loadView];
CGRect viewRect = [self viewBoundsWithOrientation:self.interfaceOrientation];
UIViewController *rootViewController = [self.viewControllers objectAtIndex:0];
[rootViewController willMoveToParentViewController:self];
[self addChildViewController:rootViewController];
UIView * rootView = rootViewController.view;
rootView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
rootView.frame = viewRect;
[self.view addSubview:rootView];
[rootViewController didMoveToParentViewController:self];
_blackMask = [[UIView alloc] initWithFrame:viewRect];
_blackMask.backgroundColor = [UIColor blackColor];
_blackMask.alpha = 0.0;
_blackMask.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
[self.view insertSubview:_blackMask atIndex:0];
self.view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
}
#pragma mark - PushViewController With Completion Block
- (void) pushViewController:(UIViewController *)viewController completion:(FlipBoardNavigationControllerCompletionBlock)handler {
_animationInProgress = YES;
viewController.view.frame = CGRectOffset(self.view.bounds, self.view.bounds.size.width, 0);
viewController.view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
_blackMask.alpha = 0.0;
[viewController willMoveToParentViewController:self];
[self addChildViewController:viewController];
[self.view bringSubviewToFront:_blackMask];
[self.view addSubview:viewController.view];
[UIView animateWithDuration:kAnimationDuration delay:kAnimationDelay options:0 animations:^{
CGAffineTransform transf = CGAffineTransformIdentity;
[self currentViewController].view.transform = CGAffineTransformScale(transf, 0.9f, 0.9f);
viewController.view.frame = self.view.bounds;
_blackMask.alpha = kMaxBlackMaskAlpha;
} completion:^(BOOL finished) {
if (finished) {
[self.viewControllers addObject:viewController];
[viewController didMoveToParentViewController:self];
_animationInProgress = NO;
_gestures = [[NSMutableArray alloc] init];
[self addPanGestureToView:[self currentViewController].view];
handler();
}
}];
}
- (void) pushViewController:(UIViewController *)viewController {
[self pushViewController:viewController completion:^{}];
}
#pragma mark - PopViewController With Completion Block
- (void) popViewControllerWithCompletion:(FlipBoardNavigationControllerCompletionBlock)handler {
_animationInProgress = YES;
if (self.viewControllers.count < 2) {
return;
}
UIViewController *currentVC = [self currentViewController];
UIViewController *previousVC = [self previousViewController];
[previousVC viewWillAppear:NO];
[UIView animateWithDuration:kAnimationDuration delay:kAnimationDelay options:0 animations:^{
currentVC.view.frame = CGRectOffset(self.view.bounds, self.view.bounds.size.width, 0);
CGAffineTransform transf = CGAffineTransformIdentity;
previousVC.view.transform = CGAffineTransformScale(transf, 1.0, 1.0);
previousVC.view.frame = self.view.bounds;
_blackMask.alpha = 0.0;
} completion:^(BOOL finished) {
if (finished) {
[currentVC.view removeFromSuperview];
[currentVC willMoveToParentViewController:nil];
[self.view bringSubviewToFront:[self previousViewController].view];
[currentVC removeFromParentViewController];
[currentVC didMoveToParentViewController:nil];
[self.viewControllers removeObject:currentVC];
_animationInProgress = NO;
[previousVC viewDidAppear:NO];
handler();
}
}];
}
- (void) popViewController {
[self popViewControllerWithCompletion:^{}];
}
- (void) rollBackViewController {
_animationInProgress = YES;
UIViewController * vc = [self currentViewController];
UIViewController * nvc = [self previousViewController];
CGRect rect = CGRectMake(0, 0, vc.view.frame.size.width, vc.view.frame.size.height);
[UIView animateWithDuration:0.3f delay:kAnimationDelay options:0 animations:^{
CGAffineTransform transf = CGAffineTransformIdentity;
nvc.view.transform = CGAffineTransformScale(transf, 0.9f, 0.9f);
vc.view.frame = rect;
_blackMask.alpha = kMaxBlackMaskAlpha;
} completion:^(BOOL finished) {
if (finished) {
_animationInProgress = NO;
}
}];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
}
#pragma mark - ChildViewController
- (UIViewController *)currentViewController {
UIViewController *result = nil;
if ([self.viewControllers count]>0) {
result = [self.viewControllers lastObject];
}
return result;
}
#pragma mark - ParentViewController
- (UIViewController *)previousViewController {
UIViewController *result = nil;
if ([self.viewControllers count]>1) {
result = [self.viewControllers objectAtIndex:self.viewControllers.count - 2];
}
return result;
}
#pragma mark - Add Pan Gesture
- (void) addPanGestureToView:(UIView*)view
{
NSLog(@"ADD PAN GESTURE $$### %i",[_gestures count]);
UIPanGestureRecognizer* panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self
action:@selector(gestureRecognizerDidPan:)];
panGesture.cancelsTouchesInView = YES;
panGesture.delegate = self;
[view addGestureRecognizer:panGesture];
[_gestures addObject:panGesture];
panGesture = nil;
}
# pragma mark - Avoid Unwanted Vertical Gesture
- (BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)panGestureRecognizer {
CGPoint translation = [panGestureRecognizer translationInView:self.view];
return fabs(translation.x) > fabs(translation.y) ;
}
#pragma mark - Gesture recognizer
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
UIViewController * vc = [self.viewControllers lastObject];
_panOrigin = vc.view.frame.origin;
gestureRecognizer.enabled = YES;
return !_animationInProgress;
}
- (BOOL) gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return NO;
}
#pragma mark - Handle Panning Activity
- (void) gestureRecognizerDidPan:(UIPanGestureRecognizer*)panGesture {
if(_animationInProgress) return;
CGPoint currentPoint = [panGesture translationInView:self.view];
CGFloat x = currentPoint.x + _panOrigin.x;
PanDirection panDirection = PanDirectionNone;
CGPoint vel = [panGesture velocityInView:self.view];
if (vel.x > 0) {
panDirection = PanDirectionRight;
} else {
panDirection = PanDirectionLeft;
}
CGFloat offset = 0;
UIViewController * vc ;
vc = [self currentViewController];
offset = CGRectGetWidth(vc.view.frame) - x;
_percentageOffsetFromLeft = offset/[self viewBoundsWithOrientation:self.interfaceOrientation].size.width;
vc.view.frame = [self getSlidingRectWithPercentageOffset:_percentageOffsetFromLeft orientation:self.interfaceOrientation];
[self transformAtPercentage:_percentageOffsetFromLeft];
if (panGesture.state == UIGestureRecognizerStateEnded || panGesture.state == UIGestureRecognizerStateCancelled) {
// If velocity is greater than 100 the Execute the Completion base on pan direction
if(abs(vel.x) > 100) {
[self completeSlidingAnimationWithDirection:panDirection];
}else {
[self completeSlidingAnimationWithOffset:offset];
}
}
}
#pragma mark - Set the required transformation based on percentage
- (void) transformAtPercentage:(CGFloat)percentage {
CGAffineTransform transf = CGAffineTransformIdentity;
CGFloat newTransformValue = 1 - (percentage*10)/100;
CGFloat newAlphaValue = percentage* kMaxBlackMaskAlpha;
[self previousViewController].view.transform = CGAffineTransformScale(transf,newTransformValue,newTransformValue);
_blackMask.alpha = newAlphaValue;
}
#pragma mark - This will complete the animation base on pan direction
- (void) completeSlidingAnimationWithDirection:(PanDirection)direction {
if(direction==PanDirectionRight){
[self popViewController];
}else {
[self rollBackViewController];
}
}
#pragma mark - This will complete the animation base on offset
- (void) completeSlidingAnimationWithOffset:(CGFloat)offset{
if(offset<[self viewBoundsWithOrientation:self.interfaceOrientation].size.width/2) {
[self popViewController];
}else {
[self rollBackViewController];
}
}
#pragma mark - Get the origin and size of the visible viewcontrollers(child)
- (CGRect) getSlidingRectWithPercentageOffset:(CGFloat)percentage orientation:(UIInterfaceOrientation)orientation {
CGRect viewRect = [self viewBoundsWithOrientation:orientation];
CGRect rectToReturn = CGRectZero;
UIViewController * vc;
vc = [self currentViewController];
rectToReturn.size = viewRect.size;
rectToReturn.origin = CGPointMake(MAX(0,(1-percentage)*viewRect.size.width), 0.0);
return rectToReturn;
}
#pragma mark - Get the size of view in the main screen
- (CGRect) viewBoundsWithOrientation:(UIInterfaceOrientation)orientation{
CGRect bounds = [UIScreen mainScreen].bounds;
if([[UIApplication sharedApplication]isStatusBarHidden]){
return bounds;
} else if(UIInterfaceOrientationIsLandscape(orientation)){
CGFloat width = bounds.size.width;
bounds.size.width = bounds.size.height;
bounds.size.height = width - 20;
return bounds;
}else{
bounds.size.height-=20;
return bounds;
}
}
@end
#pragma mark - UIViewController Category
//For Global Access of flipViewController
@implementation UIViewController (FlipBoardNavigationController)
@dynamic flipboardNavigationController;
- (FlipBoardNavigationController *)flipboardNavigationController
{
UIViewController *parentViewController = self.parentViewController;
while (parentViewController != nil) {
if([parentViewController isKindOfClass:[FlipBoardNavigationController class]]){
return (FlipBoardNavigationController *)parentViewController;
}
parentViewController = parentViewController.parentViewController;
}
return nil;
}
@end