RootViewController Switch Transition Animation RootViewController Switch Transition Animation xcode xcode

RootViewController Switch Transition Animation


You can wrap the switching of the rootViewController in a transition animation block:

[UIView transitionWithView:self.window                  duration:0.5                   options:UIViewAnimationOptionTransitionFlipFromLeft                animations:^{ self.window.rootViewController = newViewController; }                completion:nil];


I found this and works perfectly:

in your appDelegate:

- (void)changeRootViewController:(UIViewController*)viewController {    if (!self.window.rootViewController) {        self.window.rootViewController = viewController;        return;    }    UIView *snapShot = [self.window snapshotViewAfterScreenUpdates:YES];    [viewController.view addSubview:snapShot];    self.window.rootViewController = viewController;    [UIView animateWithDuration:0.5 animations:^{        snapShot.layer.opacity = 0;        snapShot.layer.transform = CATransform3DMakeScale(1.5, 1.5, 1.5);    } completion:^(BOOL finished) {        [snapShot removeFromSuperview];    }];}

in your app

 if (!app) { app = (AppDelegate *)[[UIApplication sharedApplication] delegate]; }        [app changeRootViewController:newViewController];

credits:

https://gist.github.com/gimenete/53704124583b5df3b407


I am posting Jesus answer implemented in swift. It takes viewcontroller's identifier as an argument, loads from storyboard desiredViewController and changes rootViewController with animation.

Swift 3.0 Update:

  func changeRootViewController(with identifier:String!) {    let storyboard = self.window?.rootViewController?.storyboard    let desiredViewController = storyboard?.instantiateViewController(withIdentifier: identifier);    let snapshot:UIView = (self.window?.snapshotView(afterScreenUpdates: true))!    desiredViewController?.view.addSubview(snapshot);    self.window?.rootViewController = desiredViewController;    UIView.animate(withDuration: 0.3, animations: {() in      snapshot.layer.opacity = 0;      snapshot.layer.transform = CATransform3DMakeScale(1.5, 1.5, 1.5);      }, completion: {        (value: Bool) in        snapshot.removeFromSuperview();    });  }

Swift 2.2 Update:

  func changeRootViewControllerWithIdentifier(identifier:String!) {    let storyboard = self.window?.rootViewController?.storyboard    let desiredViewController = storyboard?.instantiateViewControllerWithIdentifier(identifier);    let snapshot:UIView = (self.window?.snapshotViewAfterScreenUpdates(true))!    desiredViewController?.view.addSubview(snapshot);    self.window?.rootViewController = desiredViewController;    UIView.animateWithDuration(0.3, animations: {() in      snapshot.layer.opacity = 0;      snapshot.layer.transform = CATransform3DMakeScale(1.5, 1.5, 1.5);      }, completion: {        (value: Bool) in        snapshot.removeFromSuperview();    });  }  class func sharedAppDelegate() -> AppDelegate? {    return UIApplication.sharedApplication().delegate as? AppDelegate;  }

After, you have a very simple usage from anywhere:

let appDelegate = AppDelegate.sharedAppDelegate()appDelegate?.changeRootViewControllerWithIdentifier("YourViewControllerID")

Swift 3.0 update

let appDelegate = UIApplication.shared.delegate as! AppDelegateappDelegate.changeRootViewController(with: "listenViewController")