A single class to control transitions in iOS 7

The class can encapsulate all the transitioning animations for you navigation controller based app and leave the animation logic out of the view controllers them selves. You can write custom animations and transitions between specific view controllers.

1- First in our root view controller, we need to tell our navigation controller to use our new class.

We are going to instantiate our new class, the MainTransitioningDelegate and maintain a reference to it in our root view controller so it doesn’t get deallocated.

-(void)viewDidLoad(){ //create our transitioning delegate and set the delegate to the nav controller self.transDelegate =[[MainTransitioningDelegate alloc]init]; self.navigationController.delegate =self.transDelegate; [super viewDidLoad]; }

2- Now we are going to create our Transitioning Delegate and add code to transition between various specific view controllers

#import <Foundation/Foundation.h> #import <UIKit/UIKit.h> @interface MainTransitioningDelegate : NSObject<UIViewControllerTransitioningDelegate, UIViewControllerAnimatedTransitioning, UINavigationControllerDelegate> @end // // MainTransitioningDelegate.m // MyOwnId // // This class is designed to contain all of the transitioning logic for the main navigation controller of the // application. By assigning the navigation controller delegate to this class and maintaining a reference to the // class we are able to encapsulate all of our transitioning logic here and create custom animations for each view // controller combination // // Created by Logan Sease on 10/22/14. // Copyright (c) 2014 Logan Sease. All rights reserved. // #import "MainTransitioningDelegate.h" #import "HomeController.h" #import "CompanySearchController.h" @implementation MainTransitioningDelegate #pragma mark animation methods // Report who is in charge of the animation // To do this properly, we must return this class for all VC combinations that we will support. For all others //we must return nil to support the standard transition without interfering - (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC { if( [fromVC isKindOfClass:[HomeController class]] && [toVC isKindOfClass:[CompanySearchController class]]) { return self; } else if( [fromVC isKindOfClass:[CompanySearchController class]] && [toVC isKindOfClass:[HomeController class]]) { return self; } else{ return nil; } } - (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext { return .5; } - (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext { //get the view we are going to and take a snapshot of it and add it to the view as invisible UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey]; UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey]; //perform the home to search animation if([fromVC isKindOfClass:[HomeController class]] && [toVC isKindOfClass:[CompanySearchController class]] ) { [self animationFromHome:(HomeController*)fromVC toSearch:(CompanySearchController*)toVC withContext:transitionContext]; } else if([fromVC isKindOfClass:[CompanySearchController class]] && [toVC isKindOfClass:[HomeController class]] ) { [self animationFromSearch:(CompanySearchController*)fromVC toHome:(HomeController*)toVC withContext:transitionContext]; } } /** * This is the animation that will occur from the home to the company search controller */ -(void)animationFromHome:(HomeController*)fromVC toSearch:(CompanySearchController*)toVC withContext:(id<UIViewControllerContextTransitioning>)transitionContext { //get the container view that will hold the transition UIView *container = transitionContext.containerView; UIView *toView = toVC.view; UIView *snapshot = [toView snapshotViewAfterScreenUpdates:YES]; snapshot.alpha = 0.0f; container.backgroundColor = [UIColor whiteColor]; [container addSubview:snapshot]; //add the text box we are going to animate UIView * moveView = [fromVC.searchView snapshotViewAfterScreenUpdates:NO]; [container addSubview:moveView]; moveView.frame = fromVC.searchView.frame; //fade out the current view [UIView animateWithDuration:.2 animations:^{ fromVC.view.alpha = 0.0f; } completion:^(BOOL finished) { }]; //move the search bar up to the top, fading it out and fade the new view in [UIView animateWithDuration:.5 animations:^{ moveView.frame = CGRectMake(moveView.frame.origin.x,0, moveView.frame.size.width, moveView.frame.size.height); moveView.alpha = 0.0f; }]; //fade the next VCs view in and reset after completion. //add the transition view to our resulting container and complete the transition //also clear the nav delegate so we won't use this to transition any more. [UIView animateWithDuration:.25 delay:0.25f options:0 animations:^{ snapshot.alpha = 1.0f; } completion:^(BOOL finished) { [snapshot removeFromSuperview]; //toVC.navigationController.delegate = nil; [transitionContext completeTransition:YES]; [container addSubview:toView]; fromVC.view.alpha = 1.0f; }]; } /** * This is the animation that will occur from the search to the home controller */ -(void)animationFromSearch:(CompanySearchController*)fromVC toHome:(HomeController*)toVC withContext:(id<UIViewControllerContextTransitioning>)transitionContext { //get the container view that will hold the transition UIView *container = transitionContext.containerView; //the the views from both view controllers UIView *toView = toVC.view; UIView *toSnapshot = [toView snapshotViewAfterScreenUpdates:YES]; UIView *fromView = fromVC.view; UIView *fromSnapshot = [fromView snapshotViewAfterScreenUpdates:NO]; //add both of the views to the container view toSnapshot.alpha = 0.0f; [container addSubview:fromSnapshot]; [container addSubview:toSnapshot]; //add the text box we are going to animate UIView * moveView = [toVC.searchView snapshotViewAfterScreenUpdates:YES]; [container addSubview:moveView]; //start the move view up at the top of our window moveView.frame = CGRectMake(moveView.frame.origin.x,0, moveView.frame.size.width, moveView.frame.size.height); moveView.alpha = 1.0f; //fade out the current view [UIView animateWithDuration:.2 animations:^{ fromView.alpha = 0.0f; } completion:^(BOOL finished) { }]; //move the search bar from the top to its actual position [UIView animateWithDuration:.5 animations:^{ moveView.frame = toVC.searchView.frame; moveView.alpha = 1.0f; }]; //fade the next VCs view in and reset after completion. //add the transition view to our resulting container and complete the transition [UIView animateWithDuration:.25 delay:0.25f options:0 animations:^{ toSnapshot.alpha = 1.0f; } completion:^(BOOL finished) { [toSnapshot removeFromSuperview]; [fromSnapshot removeFromSuperview]; [moveView removeFromSuperview]; [container addSubview:toView]; [transitionContext completeTransition:YES]; }]; } @end

Comments

  Comments

This site uses Akismet to reduce spam. Learn how your comment data is processed.

-->