Instantly share code, notes, and snippets.
Last active
December 29, 2015 02:59
-
Star
(0)
0
You must be signed in to star a gist -
Fork
(0)
0
You must be signed in to fork a gist
-
Save bentford/7604589 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// CustomNavViewController.h | |
// | |
// | |
// Created by Ben Ford on 11/28/12. | |
// Copyright (c) 2012 Ben Ford. All rights reserved. | |
// | |
#import <UIKit/UIKit.h> | |
typedef enum { | |
NavigationDirectionLeft, | |
NavigationDirectionRight, | |
} NavigationDirection; | |
@interface CustomNavigationViewController : UIViewController | |
@property (nonatomic, retain) UIImage *backgroundImage; | |
- (void)pushViewController:(UIViewController *)childViewController direction:(NavigationDirection)navigationDirection animated:(BOOL)animated; | |
- (void)popViewControllerAnimated:(BOOL)animated; | |
@end | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// CustomNavViewController.m | |
// | |
// | |
// Created by Ben Ford on 11/28/12. | |
// Copyright (c) 2012 Ben Ford. All rights reserved. | |
// | |
#import "CustomNavigationViewController.h" | |
#define kAnimationSpeed 0.35f | |
@interface CustomNavigationViewController () | |
@end | |
@implementation CustomNavigationViewController | |
{ | |
UIImageView *backgroundImageView; | |
NSMutableArray *childViewControllers; | |
NSMutableArray *pushDirections; | |
} | |
- (id)init { | |
if( (self = [super init]) ) { | |
childViewControllers = [[NSMutableArray alloc] init]; | |
pushDirections = [[NSMutableArray alloc] init]; | |
} | |
return self; | |
} | |
- (void)viewDidLoad { | |
[super viewDidLoad]; | |
if (self.backgroundImage) { | |
backgroundImageView = [[UIImageView alloc] initWithImage:self.backgroundImage]; | |
[self.view addSubview:backgroundImageView]; | |
} | |
} | |
- (void)setBackgroundImage:(UIImage *)backgroundImage | |
{ | |
_backgroundImage = backgroundImage; | |
if (backgroundImageView) { | |
backgroundImageView.image = backgroundImage; | |
} | |
} | |
- (void)pushViewController:(UIViewController *)newViewController direction:(NavigationDirection)navigationDirection animated:(BOOL)animated { | |
// setup | |
UIViewController *oldViewController = [childViewControllers lastObject]; | |
[childViewControllers addObject:newViewController]; | |
[pushDirections addObject:[NSNumber numberWithInt:navigationDirection]]; | |
// step 1: | |
[self addChildViewController:newViewController]; | |
[oldViewController willMoveToParentViewController:self]; | |
// step 2: | |
if( animated == YES ) { | |
CGFloat startX = navigationDirection == NavigationDirectionLeft ? self.view.frame.size.width : -self.view.frame.size.width; | |
CGFloat endX = navigationDirection == NavigationDirectionLeft ? -self.view.frame.size.width : self.view.frame.size.width; | |
newViewController.view.frame = CGRectMake(startX, 0, self.view.frame.size.width, self.view.frame.size.height); | |
[self transitionFromViewController:oldViewController toViewController:newViewController duration:kAnimationSpeed options:0 animations:^{ | |
oldViewController.view.frame = CGRectMake(endX, 0, oldViewController.view.frame.size.width, oldViewController.view.frame.size.height); | |
newViewController.view.frame = CGRectMake(0, 0, newViewController.view.frame.size.width, newViewController.view.frame.size.height); | |
} completion:^(BOOL finished) { | |
[oldViewController removeFromParentViewController]; | |
[newViewController didMoveToParentViewController:self]; | |
}]; | |
} else { | |
newViewController.view.frame = self.view.bounds; | |
[self.view addSubview:newViewController.view]; | |
[oldViewController.view removeFromSuperview]; | |
[oldViewController removeFromParentViewController]; | |
} | |
// step 3: | |
[newViewController didMoveToParentViewController:self]; | |
} | |
- (void)popViewControllerAnimated:(BOOL)animated { | |
// setup: | |
UIViewController *oldViewController = [childViewControllers lastObject]; | |
[childViewControllers removeLastObject]; | |
UIViewController *newViewController = [childViewControllers lastObject]; | |
NavigationDirection removeDirection = (NavigationDirection)[[pushDirections lastObject] intValue]; | |
[pushDirections removeLastObject]; | |
// flip the directions so the animation goes the opposite way | |
removeDirection = removeDirection == NavigationDirectionLeft ? NavigationDirectionRight : NavigationDirectionLeft; | |
// step 1: | |
[oldViewController willMoveToParentViewController:nil]; | |
if( newViewController != nil ) | |
[self addChildViewController:newViewController]; | |
// step 2: | |
if( animated == YES ) { | |
CGFloat startX = removeDirection == NavigationDirectionLeft ? self.view.frame.size.width : -self.view.frame.size.width; | |
CGFloat endX = removeDirection == NavigationDirectionLeft ? -self.view.frame.size.width : self.view.frame.size.width; | |
newViewController.view.frame = CGRectMake(startX, 0, newViewController.view.frame.size.width, newViewController.view.frame.size.height); | |
[self transitionFromViewController:oldViewController toViewController:newViewController duration:kAnimationSpeed options:0 animations:^{ | |
oldViewController.view.frame = CGRectMake(endX, 0, oldViewController.view.frame.size.width, oldViewController.view.frame.size.height); | |
newViewController.view.frame = CGRectMake(0, 0, newViewController.view.frame.size.width, newViewController.view.frame.size.height); | |
} completion:^(BOOL finished) { | |
[oldViewController removeFromParentViewController]; | |
[newViewController didMoveToParentViewController:self]; | |
}]; | |
} else { | |
// step 3: | |
newViewController.view.frame = self.view.bounds; | |
[self.view addSubview:newViewController.view]; | |
[oldViewController.view removeFromSuperview]; | |
[oldViewController removeFromParentViewController]; | |
} | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment