导航控制器,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

你可能感兴趣的:(ios)