How to change the Push and Pop animations in a navigation based app How to change the Push and Pop animations in a navigation based app ios ios

How to change the Push and Pop animations in a navigation based app


I did the following and it works fine.. and is simple and easy to understand..

CATransition* transition = [CATransition animation];transition.duration = 0.5;transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];transition.type = kCATransitionFade; //kCATransitionMoveIn; //, kCATransitionPush, kCATransitionReveal, kCATransitionFade//transition.subtype = kCATransitionFromTop; //kCATransitionFromLeft, kCATransitionFromRight, kCATransitionFromTop, kCATransitionFromBottom[self.navigationController.view.layer addAnimation:transition forKey:nil];[[self navigationController] popViewControllerAnimated:NO];

And the same thing for push..


Swift 3.0 version:

let transition = CATransition()transition.duration = 0.5transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)transition.type = kCATransitionFadeself.navigationController?.view.layer.add(transition, forKey: nil)_ = self.navigationController?.popToRootViewController(animated: false)


This is how I've always managed to complete this task.

For Push:

MainView *nextView=[[MainView alloc] init];[UIView  beginAnimations:nil context:NULL];[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];[UIView setAnimationDuration:0.75];[self.navigationController pushViewController:nextView animated:NO];[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.navigationController.view cache:NO];[UIView commitAnimations];[nextView release];

For Pop:

[UIView  beginAnimations:nil context:NULL];[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];[UIView setAnimationDuration:0.75];[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.navigationController.view cache:NO];[UIView commitAnimations];[UIView beginAnimations:nil context:NULL];[UIView setAnimationDelay:0.375];[self.navigationController popViewControllerAnimated:NO];[UIView commitAnimations];


I still get a lot of feedback from this so I'm going to go ahead and update it to use animation blocks which is the Apple recommended way to do animations anyway.

For Push:

MainView *nextView = [[MainView alloc] init];[UIView animateWithDuration:0.75                         animations:^{                             [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];                             [self.navigationController pushViewController:nextView animated:NO];                             [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.navigationController.view cache:NO];                         }];

For Pop:

[UIView animateWithDuration:0.75                         animations:^{                             [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];                             [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.navigationController.view cache:NO];                         }];[self.navigationController popViewControllerAnimated:NO];


How to change the Push and Pop animations in a navigation based app...

Preamble:

Say you are new to iOS development. Confusingly, Apple provide two transitions which can be used easily. These are: "crossfade" and "flip".

But of course, "crossfade" and "flip" are useless. They are never used. Nobody knows why Apple provided those two useless transitions!

So:

Say you want to do an ordinary, common, transition such as "slide". In that case, you have to do a HUGE amount of work!.

That work, is explained in this post.

Just to repeat:

Surprisingly: with iOS, if you want the simplest, most common, everyday transitions (such as an ordinary slide), you do have to all the work of implementing a full custom transition.

Here's how to do it ...

1. You need a custom UIViewControllerAnimatedTransitioning

  1. You need a bool of your own like popStyle . (Is it popping on, or popping off?)

  2. You must include transitionDuration (trivial) and the main call, animateTransition

  3. In fact you must write two different routines for inside animateTransition. One for the push, and one for the pop. Probably name them animatePush and animatePop. Inside animateTransition, just branch on popStyle to the two routines

  4. The example below does a simple move-over/move-off

  5. In your animatePush and animatePop routines. You must get the "from view" and the "to view". (How to do that, is shown in the code example.)

  6. and you must addSubview for the new "to" view.

  7. and you must call completeTransition at the end of your anime

So ..

  class SimpleOver: NSObject, UIViewControllerAnimatedTransitioning {                var popStyle: Bool = false                func transitionDuration(            using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {            return 0.20        }                func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {                        if popStyle {                                animatePop(using: transitionContext)                return            }                        let fz = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.from)!            let tz = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to)!                        let f = transitionContext.finalFrame(for: tz)                        let fOff = f.offsetBy(dx: f.width, dy: 55)            tz.view.frame = fOff                        transitionContext.containerView.insertSubview(tz.view, aboveSubview: fz.view)                        UIView.animate(                withDuration: transitionDuration(using: transitionContext),                animations: {                    tz.view.frame = f            }, completion: {_ in                     transitionContext.completeTransition(true)            })        }                func animatePop(using transitionContext: UIViewControllerContextTransitioning) {                        let fz = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.from)!            let tz = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to)!                        let f = transitionContext.initialFrame(for: fz)            let fOffPop = f.offsetBy(dx: f.width, dy: 55)                        transitionContext.containerView.insertSubview(tz.view, belowSubview: fz.view)                        UIView.animate(                withDuration: transitionDuration(using: transitionContext),                animations: {                    fz.view.frame = fOffPop            }, completion: {_ in                     transitionContext.completeTransition(true)            })        }    }

And then ...

2. Use it in your view controller.

Note: strangely, you only have to do this in the "first" view controller. (The one which is "underneath".)

With the one that you pop on top, do nothing. Easy.

So your class...

class SomeScreen: UIViewController {}

becomes...

class FrontScreen: UIViewController,        UIViewControllerTransitioningDelegate, UINavigationControllerDelegate {        let simpleOver = SimpleOver()        override func viewDidLoad() {                super.viewDidLoad()        navigationController?.delegate = self    }    func navigationController(        _ navigationController: UINavigationController,        animationControllerFor operation: UINavigationControllerOperation,        from fromVC: UIViewController,        to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {                simpleOver.popStyle = (operation == .pop)        return simpleOver    }}

That's it.

Push and pop exactly as normal, no change. To push ...

let n = UIStoryboard(name: "nextScreenStoryboardName", bundle: nil)          .instantiateViewController(withIdentifier: "nextScreenStoryboardID")          as! NextScreennavigationController?.pushViewController(n, animated: true)

and to pop it, you can if you like just do that on the next screen:

class NextScreen: TotallyOrdinaryUIViewController {        @IBAction func userClickedBackOrDismissOrSomethingLikeThat() {                navigationController?.popViewController(animated: true)    }}

Phew.


3. Also enjoy the other answers on this page which explain how to override AnimatedTransitioning !

Scroll to @AlanZeino and @elias answer for more discussion on how to AnimatedTransitioning in iOS apps these days!