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

与 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

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理