关于ios:导航控制器pushViewController-的转场动画

39次阅读

共计 1501 个字符,预计需要花费 4 分钟才能阅读完成。

与 ViewController 的 present 转场动画不同

1, 导航控制器,pushViewController 的转场动画,的代理是

navigationController?.delegate = self

2, 指定动画的办法

extension Two: UINavigationControllerDelegate{func navigationController(_ navigationController: UINavigationController, animationControllerFor operation: UINavigationController.Operation, from fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        if operation == .push{return navAnimatorPush}
        else{return nil}
    }
    
}

具体的动画设置也不同

应用 snapshotView , 会呈现奇怪的成果

代码很好了解


class NavBaseCustomAnimatorPush: NSObject, UIViewControllerAnimatedTransitioning{func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {return 1}
    
    
    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {guard let fromCtrl = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.from),
               let toCtrl = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to), let fromView = fromCtrl.view, let toView = toCtrl.view else{return}
         
        
        let containerView = transitionContext.containerView
        let f = UIScreen.main.bounds
        
        fromView.frame = f

        containerView.addSubview(fromView)
        containerView.addSubview(toView)
        
        toView.frame = presentingDirection.offsetF(withFrame: f)
        
         UIView.animate(withDuration: transitionDuration(using: transitionContext), delay: 0.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.0, options: .curveLinear) {toView.frame = f} completion: { _ in
             let success = !transitionContext.transitionWasCancelled
             transitionContext.completeTransition(success)
         }
     }
    
}

github repo

正文完
 0