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

前端 未结 25 1590
清酒与你
清酒与你 2020-11-22 12:46

I have a navigation based application and I want to change the animation of the push and pop animations. How would I do that?

Edit 2018

Ther

25条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-22 12:59

    There are UINavigationControllerDelegate and UIViewControllerAnimatedTransitioning there you can change animation for anything you want.

    For example this is vertical pop animation for VC:

    @objc class PopAnimator: NSObject, UIViewControllerAnimatedTransitioning {
    
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
        return 0.5
    }
    
    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
    
        let fromViewController = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey)!
        let toViewController = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)!
        let containerView = transitionContext.containerView()
        let bounds = UIScreen.mainScreen().bounds
        containerView!.insertSubview(toViewController.view, belowSubview: fromViewController.view)
        toViewController.view.alpha = 0.5
    
        let finalFrameForVC = fromViewController.view.frame
    
        UIView.animateWithDuration(transitionDuration(transitionContext), animations: {
            fromViewController.view.frame = CGRectOffset(finalFrameForVC, 0, bounds.height)
            toViewController.view.alpha = 1.0
            }, completion: {
                finished in
                transitionContext.completeTransition(!transitionContext.transitionWasCancelled())
        })
    }
    

    }

    And then

    func navigationController(navigationController: UINavigationController, animationControllerForOperation operation: UINavigationControllerOperation, fromViewController fromVC: UIViewController, toViewController toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        if operation == .Pop {
            return PopAnimator()
        }
        return nil;
    }
    

    Useful tutorial https://www.objc.io/issues/5-ios7/view-controller-transitions/

提交回复
热议问题