-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDDMenuController.m
More file actions
executable file
·750 lines (529 loc) · 24.3 KB
/
DDMenuController.m
File metadata and controls
executable file
·750 lines (529 loc) · 24.3 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
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
//
// DDMenuController.m
// DDMenuController
//
// Created by Devin Doty on 11/30/11.
// Copyright (c) 2011 toaast. All rights reserved.
//
// 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 "DDMenuController.h"
#import "QuartzCore/QuartzCore.h"
#define kMenuFullWidth 320.0f
#define kMenuDisplayedWidth 280.0f
#define kMenuOverlayWidth (self.view.bounds.size.width - kMenuDisplayedWidth)
#define kMenuBounceOffset 10.0f
#define kMenuBounceDuration .3f
#define kMenuSlideDuration .3f
@interface DDMenuController (Internal)
- (void)showShadow:(BOOL)val;
@end
@implementation DDMenuController
@synthesize delegate, barButtonItemClass;
@synthesize leftViewController=_left;
@synthesize rightViewController=_right;
@synthesize rootViewController=_root;
@synthesize tap=_tap;
@synthesize pan=_pan;
- (id)initWithRootViewController:(UIViewController*)controller {
if ((self = [self init])) {
_root = controller;
}
return self;
}
- (id)init {
if ((self = [super init])) {
self.barButtonItemClass = [UIBarButtonItem class];
}
return self;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
#pragma mark - View lifecycle
- (void)viewDidLoad {
[super viewDidLoad];
[self setRootViewController:_root]; // reset root
if (!_tap) {
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
tap.delegate = (id<UIGestureRecognizerDelegate>)self;
[self.view addGestureRecognizer:tap];
[tap setEnabled:NO];
_tap = tap;
}
}
- (void)viewDidUnload {
[super viewDidUnload];
_tap = nil;
_pan = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
return [_root shouldAutorotateToInterfaceOrientation:toInterfaceOrientation];
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
[super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
if (_root) {
[_root willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
UIView *view = _root.view;
if (_menuFlags.showingRightView) {
view.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;
} else if (_menuFlags.showingLeftView) {
view.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
} else {
view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
}
}
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
[super didRotateFromInterfaceOrientation:fromInterfaceOrientation];
if (_root) {
[_root didRotateFromInterfaceOrientation:fromInterfaceOrientation];
CGRect frame = self.view.bounds;
if (_menuFlags.showingLeftView) {
frame.origin.x = frame.size.width - kMenuOverlayWidth;
} else if (_menuFlags.showingRightView) {
frame.origin.x = -(frame.size.width - kMenuOverlayWidth);
}
_root.view.frame = frame;
_root.view.autoresizingMask = self.view.autoresizingMask;
[self showShadow:(_root.view.layer.shadowOpacity!=0.0f)];
}
}
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
[super willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration];
if (_root) {
[_root willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration];
}
}
#pragma mark - GestureRecognizers
- (void)pan:(UIPanGestureRecognizer*)gesture {
if (gesture.state == UIGestureRecognizerStateBegan) {
[self showShadow:YES];
_panOriginX = self.view.frame.origin.x;
_panVelocity = CGPointMake(0.0f, 0.0f);
if([gesture velocityInView:self.view].x > 0) {
_panDirection = DDMenuPanDirectionRight;
} else {
_panDirection = DDMenuPanDirectionLeft;
}
}
if (gesture.state == UIGestureRecognizerStateChanged) {
CGPoint velocity = [gesture velocityInView:self.view];
if((velocity.x*_panVelocity.x + velocity.y*_panVelocity.y) < 0) {
_panDirection = (_panDirection == DDMenuPanDirectionRight) ? DDMenuPanDirectionLeft : DDMenuPanDirectionRight;
}
_panVelocity = velocity;
CGPoint translation = [gesture translationInView:self.view];
CGRect frame = _root.view.frame;
frame.origin.x = _panOriginX + translation.x;
if (frame.origin.x > 0.0f && !_menuFlags.showingLeftView) {
if(_menuFlags.showingRightView) {
_menuFlags.showingRightView = NO;
[self.rightViewController.view removeFromSuperview];
}
if (_menuFlags.canShowLeft) {
_menuFlags.showingLeftView = YES;
CGRect frame = self.view.bounds;
frame.size.width = kMenuFullWidth;
self.leftViewController.view.frame = frame;
[self.view insertSubview:self.leftViewController.view atIndex:0];
} else {
frame.origin.x = 0.0f; // ignore right view if it's not set
}
} else if (frame.origin.x < 0.0f && !_menuFlags.showingRightView) {
if(_menuFlags.showingLeftView) {
_menuFlags.showingLeftView = NO;
[self.leftViewController.view removeFromSuperview];
}
if (_menuFlags.canShowRight) {
_menuFlags.showingRightView = YES;
CGRect frame = self.view.bounds;
frame.origin.x += frame.size.width - kMenuFullWidth;
frame.size.width = kMenuFullWidth;
self.rightViewController.view.frame = frame;
[self.view insertSubview:self.rightViewController.view atIndex:0];
} else {
frame.origin.x = 0.0f; // ignore left view if it's not set
}
}
_root.view.frame = frame;
} else if (gesture.state == UIGestureRecognizerStateEnded || gesture.state == UIGestureRecognizerStateCancelled) {
// Finishing moving to left, right or root view with current pan velocity
[self.view setUserInteractionEnabled:NO];
DDMenuPanCompletion completion = DDMenuPanCompletionRoot; // by default animate back to the root
if (_panDirection == DDMenuPanDirectionRight && _menuFlags.showingLeftView) {
completion = DDMenuPanCompletionLeft;
} else if (_panDirection == DDMenuPanDirectionLeft && _menuFlags.showingRightView) {
completion = DDMenuPanCompletionRight;
}
CGPoint velocity = [gesture velocityInView:self.view];
if (velocity.x < 0.0f) {
velocity.x *= -1.0f;
}
BOOL bounce = (velocity.x > 800);
CGFloat originX = _root.view.frame.origin.x;
CGFloat width = _root.view.frame.size.width;
CGFloat span = (width - kMenuOverlayWidth);
CGFloat duration = kMenuSlideDuration; // default duration with 0 velocity
if (bounce) {
duration = (span / velocity.x); // bouncing we'll use the current velocity to determine duration
} else {
duration = ((span - originX) / span) * duration; // user just moved a little, use the defult duration, otherwise it would be too slow
}
[CATransaction begin];
[CATransaction setCompletionBlock:^{
if (completion == DDMenuPanCompletionLeft) {
[self showLeftController:NO];
} else if (completion == DDMenuPanCompletionRight) {
[self showRightController:NO];
} else {
[self showRootController:NO];
}
[_root.view.layer removeAllAnimations];
[self.view setUserInteractionEnabled:YES];
}];
CGPoint pos = _root.view.layer.position;
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
NSMutableArray *keyTimes = [[NSMutableArray alloc] initWithCapacity:bounce ? 3 : 2];
NSMutableArray *values = [[NSMutableArray alloc] initWithCapacity:bounce ? 3 : 2];
NSMutableArray *timingFunctions = [[NSMutableArray alloc] initWithCapacity:bounce ? 3 : 2];
[values addObject:[NSValue valueWithCGPoint:pos]];
[timingFunctions addObject:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
[keyTimes addObject:[NSNumber numberWithFloat:0.0f]];
if (bounce) {
duration += kMenuBounceDuration;
[keyTimes addObject:[NSNumber numberWithFloat:1.0f - ( kMenuBounceDuration / duration)]];
if (completion == DDMenuPanCompletionLeft) {
[values addObject:[NSValue valueWithCGPoint:CGPointMake(((width/2) + span) + kMenuBounceOffset, pos.y)]];
} else if (completion == DDMenuPanCompletionRight) {
[values addObject:[NSValue valueWithCGPoint:CGPointMake(-((width/2) - (kMenuOverlayWidth-kMenuBounceOffset)), pos.y)]];
} else {
// depending on which way we're panning add a bounce offset
if (_panDirection == DDMenuPanDirectionLeft) {
[values addObject:[NSValue valueWithCGPoint:CGPointMake((width/2) - kMenuBounceOffset, pos.y)]];
} else {
[values addObject:[NSValue valueWithCGPoint:CGPointMake((width/2) + kMenuBounceOffset, pos.y)]];
}
}
[timingFunctions addObject:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
}
if (completion == DDMenuPanCompletionLeft) {
[values addObject:[NSValue valueWithCGPoint:CGPointMake((width/2) + span, pos.y)]];
} else if (completion == DDMenuPanCompletionRight) {
[values addObject:[NSValue valueWithCGPoint:CGPointMake(-((width/2) - kMenuOverlayWidth), pos.y)]];
} else {
[values addObject:[NSValue valueWithCGPoint:CGPointMake(width/2, pos.y)]];
}
[timingFunctions addObject:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
[keyTimes addObject:[NSNumber numberWithFloat:1.0f]];
animation.timingFunctions = timingFunctions;
animation.keyTimes = keyTimes;
//animation.calculationMode = @"cubic";
animation.values = values;
animation.duration = duration;
animation.removedOnCompletion = NO;
animation.fillMode = kCAFillModeForwards;
[_root.view.layer addAnimation:animation forKey:nil];
[CATransaction commit];
}
}
- (void)tap:(UITapGestureRecognizer*)gesture {
[gesture setEnabled:NO];
[self showRootController:YES];
}
#pragma mark - UIGestureRecognizerDelegate
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
// Check for horizontal pan gesture
if (gestureRecognizer == _pan) {
UIPanGestureRecognizer *panGesture = (UIPanGestureRecognizer*)gestureRecognizer;
CGPoint translation = [panGesture translationInView:self.view];
if ([panGesture velocityInView:self.view].x < 600 && sqrt(translation.x * translation.x) / sqrt(translation.y * translation.y) > 1) {
return YES;
}
return NO;
}
if (gestureRecognizer == _tap) {
if (_root && (_menuFlags.showingRightView || _menuFlags.showingLeftView)) {
return CGRectContainsPoint(_root.view.frame, [gestureRecognizer locationInView:self.view]);
}
return NO;
}
return YES;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
if (gestureRecognizer==_tap) {
return YES;
}
return NO;
}
#pragma Internal Nav Handling
- (void)resetNavButtons {
if (!_root) return;
UIViewController *topController = nil;
if ([_root isKindOfClass:[UINavigationController class]]) {
UINavigationController *navController = (UINavigationController*)_root;
if ([[navController viewControllers] count] > 0) {
topController = [[navController viewControllers] objectAtIndex:0];
}
} else if ([_root isKindOfClass:[UITabBarController class]]) {
UITabBarController *tabController = (UITabBarController*)_root;
topController = [tabController selectedViewController];
} else {
topController = _root;
}
if (_menuFlags.canShowLeft) {
UIBarButtonItem *button = [[barButtonItemClass alloc] initWithImage:[UIImage imageNamed:@"nav_menu_icon.png"] style:UIBarButtonItemStyleBordered target:self action:@selector(showLeft:)];
topController.navigationItem.leftBarButtonItem = button;
} else {
if(topController.navigationItem.leftBarButtonItem.target == self) {
topController.navigationItem.leftBarButtonItem = nil;
}
}
if (_menuFlags.canShowRight) {
UIBarButtonItem *button = [[barButtonItemClass alloc] initWithImage:[UIImage imageNamed:@"nav_menu_icon.png"] style:UIBarButtonItemStyleBordered target:self action:@selector(showRight:)];
topController.navigationItem.rightBarButtonItem = button;
} else {
if(topController.navigationItem.rightBarButtonItem.target == self) {
topController.navigationItem.rightBarButtonItem = nil;
}
}
}
- (void)showShadow:(BOOL)val {
if (!_root) return;
_root.view.layer.shadowOpacity = val ? 0.8f : 0.0f;
if (val) {
_root.view.layer.cornerRadius = 4.0f;
_root.view.layer.shadowOffset = CGSizeZero;
_root.view.layer.shadowRadius = 4.0f;
_root.view.layer.shadowPath = [UIBezierPath bezierPathWithRect:self.view.bounds].CGPath;
}
}
- (void)showRootController:(BOOL)animated {
[_tap setEnabled:NO];
_root.view.userInteractionEnabled = YES;
CGRect frame = _root.view.frame;
frame.origin.x = 0.0f;
BOOL _enabled = [UIView areAnimationsEnabled];
if (!animated) {
[UIView setAnimationsEnabled:NO];
}
[UIView animateWithDuration:.3 animations:^{
_root.view.frame = frame;
} completion:^(BOOL finished) {
if (_left && _left.view.superview) {
[_left.view removeFromSuperview];
}
if (_right && _right.view.superview) {
[_right.view removeFromSuperview];
}
_menuFlags.showingLeftView = NO;
_menuFlags.showingRightView = NO;
[self showShadow:NO];
}];
if (!animated) {
[UIView setAnimationsEnabled:_enabled];
}
}
- (void)showLeftController:(BOOL)animated {
if (!_menuFlags.canShowLeft) return;
if (_right && _right.view.superview) {
[_right.view removeFromSuperview];
_menuFlags.showingRightView = NO;
}
if (_menuFlags.respondsToWillShowViewController) {
[self.delegate menuController:self willShowViewController:self.leftViewController];
}
_menuFlags.showingLeftView = YES;
[self showShadow:YES];
UIView *view = self.leftViewController.view;
CGRect frame = self.view.bounds;
frame.size.width = kMenuFullWidth;
view.frame = frame;
[self.view insertSubview:view atIndex:0];
[self.leftViewController viewWillAppear:animated];
frame = _root.view.frame;
frame.origin.x = CGRectGetMaxX(view.frame) - (kMenuFullWidth - kMenuDisplayedWidth);
BOOL _enabled = [UIView areAnimationsEnabled];
if (!animated) {
[UIView setAnimationsEnabled:NO];
}
_root.view.userInteractionEnabled = NO;
[UIView animateWithDuration:.3 animations:^{
_root.view.frame = frame;
} completion:^(BOOL finished) {
[_tap setEnabled:YES];
}];
if (!animated) {
[UIView setAnimationsEnabled:_enabled];
}
}
- (void)showRightController:(BOOL)animated {
if (!_menuFlags.canShowRight) return;
if (_left && _left.view.superview) {
[_left.view removeFromSuperview];
_menuFlags.showingLeftView = NO;
}
if (_menuFlags.respondsToWillShowViewController) {
[self.delegate menuController:self willShowViewController:self.rightViewController];
}
_menuFlags.showingRightView = YES;
[self showShadow:YES];
UIView *view = self.rightViewController.view;
CGRect frame = self.view.bounds;
frame.origin.x += frame.size.width - kMenuFullWidth;
frame.size.width = kMenuFullWidth;
view.frame = frame;
[self.view insertSubview:view atIndex:0];
frame = _root.view.frame;
frame.origin.x = -(frame.size.width - kMenuOverlayWidth);
BOOL _enabled = [UIView areAnimationsEnabled];
if (!animated) {
[UIView setAnimationsEnabled:NO];
}
_root.view.userInteractionEnabled = NO;
[UIView animateWithDuration:.3 animations:^{
_root.view.frame = frame;
} completion:^(BOOL finished) {
[_tap setEnabled:YES];
}];
if (!animated) {
[UIView setAnimationsEnabled:_enabled];
}
}
#pragma mark Setters
- (void)setDelegate:(id<DDMenuControllerDelegate>)val {
delegate = val;
_menuFlags.respondsToWillShowViewController = [(id)self.delegate respondsToSelector:@selector(menuController:willShowViewController:)];
}
- (void)setRightViewController:(UIViewController *)rightController {
_right = rightController;
_menuFlags.canShowRight = (_right!=nil);
[self resetNavButtons];
}
- (void)setLeftViewController:(UIViewController *)leftController {
_left = leftController;
_menuFlags.canShowLeft = (_left!=nil);
[self resetNavButtons];
}
- (void)setRootViewController:(UIViewController *)rootViewController {
UIViewController *tempRoot = _root;
_root = rootViewController;
if (_root) {
if (tempRoot) {
[tempRoot.view removeFromSuperview];
tempRoot = nil;
}
UIView *view = _root.view;
view.frame = self.view.bounds;
[self.view addSubview:view];
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
pan.delegate = (id<UIGestureRecognizerDelegate>)self;
[view addGestureRecognizer:pan];
_pan = pan;
} else {
if (tempRoot) {
[tempRoot.view removeFromSuperview];
tempRoot = nil;
}
}
[self resetNavButtons];
}
- (void)setRootController:(UIViewController *)controller animated:(BOOL)animated {
if (!controller) {
[self setRootViewController:controller];
return;
}
if (_menuFlags.showingLeftView) {
[[UIApplication sharedApplication] beginIgnoringInteractionEvents];
// slide out then come back with the new root
__block DDMenuController *selfRef = self;
__block UIViewController *rootRef = _root;
CGRect frame = rootRef.view.frame;
frame.origin.x = rootRef.view.bounds.size.width;
[UIView animateWithDuration:.1 animations:^{
rootRef.view.frame = frame;
} completion:^(BOOL finished) {
[[UIApplication sharedApplication] endIgnoringInteractionEvents];
[selfRef setRootViewController:controller];
_root.view.frame = frame;
[selfRef showRootController:animated];
}];
} else {
// just add the root and move to it if it's not center
[self setRootViewController:controller];
[self showRootController:animated];
}
}
#pragma mark - Root Controller Navigation
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
NSAssert((_root!=nil), @"no root controller set");
UINavigationController *navController = nil;
if ([_root isKindOfClass:[UINavigationController class]]) {
navController = (UINavigationController*)_root;
} else if ([_root isKindOfClass:[UITabBarController class]]) {
UIViewController *topController = [(UITabBarController*)_root selectedViewController];
if ([topController isKindOfClass:[UINavigationController class]]) {
navController = (UINavigationController*)topController;
}
}
if (navController == nil) {
NSLog(@"root controller is not a navigation controller.");
return;
}
if (_menuFlags.showingRightView) {
// if we're showing the right it works a bit different, we'll make a screen shot of the menu overlay, then push, and move everything over
__block CALayer *layer = [CALayer layer];
CGRect layerFrame = self.view.bounds;
layer.frame = layerFrame;
UIGraphicsBeginImageContextWithOptions(layerFrame.size, YES, 0);
CGContextRef ctx = UIGraphicsGetCurrentContext();
[self.view.layer renderInContext:ctx];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
layer.contents = (id)image.CGImage;
[self.view.layer addSublayer:layer];
[navController pushViewController:viewController animated:NO];
CGRect frame = _root.view.frame;
frame.origin.x = frame.size.width;
_root.view.frame = frame;
frame.origin.x = 0.0f;
CGAffineTransform currentTransform = self.view.transform;
[UIView animateWithDuration:0.25f animations:^{
if (UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation])) {
self.view.transform = CGAffineTransformConcat(currentTransform, CGAffineTransformMakeTranslation(0, -[[UIScreen mainScreen] applicationFrame].size.height));
} else {
self.view.transform = CGAffineTransformConcat(currentTransform, CGAffineTransformMakeTranslation(-[[UIScreen mainScreen] applicationFrame].size.width, 0));
}
} completion:^(BOOL finished) {
[self showRootController:NO];
self.view.transform = CGAffineTransformConcat(currentTransform, CGAffineTransformMakeTranslation(0.0f, 0.0f));
[layer removeFromSuperlayer];
}];
} else {
[navController pushViewController:viewController animated:animated];
}
}
#pragma mark - Actions
- (void)showLeft:(id)sender {
[self showLeftController:YES];
}
- (void)showRight:(id)sender {
[self showRightController:YES];
}
@end