如何将 UINavigationControllerDelegate 中的自定义过渡动画应用于特定的推送和弹出事件?

Posted

技术标签:

【中文标题】如何将 UINavigationControllerDelegate 中的自定义过渡动画应用于特定的推送和弹出事件?【英文标题】:How to apply custom transition animation in UINavigationControllerDelegate to specific push and pop events? 【发布时间】:2017-08-27 23:31:20 【问题描述】:

我在 UINavigationController 堆栈中有一个用于推送和弹出事件的自定义动画转换,我想将该自定义动画仅应用于特定的推送事件 - 例如,在推送 RandomViewController44() 或弹出 RandomViewController27() 时 - 不是每个压入和弹出堆栈。是在 UINavigationControllerDelegate 中、在 animator 对象中还是在推送/弹出点处完成的? 代表

extension BaseViewController: UINavigationControllerDelegate 

    func navigationController(_ navigationController: UINavigationController, animationControllerFor operation: UINavigationControllerOperation, from fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? 
        switch operation 
        case .push:
            return InAnimator()
        case .pop:
            return OutAnimator()
        default:
            return nil
        
    


动画对象

class InAnimator: NSObject, UIViewControllerAnimatedTransitioning 

    let animationDuration = 0.5

    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval 
        return animationDuration
    

    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) 

        let screenHeight = UIScreen.main.bounds.height
        let screenWidth = UIScreen.main.bounds.width
        let containerView = transitionContext.containerView
        let toViewController = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to)

        containerView.addSubview(toViewController!.view)

        toViewController!.view.frame = CGRect(x: screenWidth * -1, y: 0, width: screenWidth, height: screenHeight)

        UIView.animate(withDuration: animationDuration, animations: 
            toViewController!.view.frame = CGRect(x: 0, y: 0, width: screenWidth, height: screenHeight)
        , completion:  finished in
            let cancelled = transitionContext.transitionWasCancelled
            transitionContext.completeTransition(!cancelled)
        )

    


对推送的调用

func pushVC() 
    navigationController?.pushViewController(randomViewController44(), animated: true)

【问题讨论】:

【参考方案1】:

您可以在下面的委托方法中检查 fromView 和 ToView 控制器以执行操作

extension BaseViewController: UINavigationControllerDelegate 

    func navigationController(_ navigationController: UINavigationController, animationControllerFor operation: UINavigationControllerOperation, from fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? 
        switch operation 
        case .push:
            if toVc is RandomViewController44
               return InAnimator()
            
            return nil
        case .pop:
            if RandomViewController27 is fromVC
              return OutAnimator()
            
            return nil
        default:
            return nil
        
    

当此方法根据您的要求作为设置条件调用时,您需要检查 from 和 toView 控制器

【讨论】:

以上是关于如何将 UINavigationControllerDelegate 中的自定义过渡动画应用于特定的推送和弹出事件?的主要内容,如果未能解决你的问题,请参考以下文章

ID:[...] 的 NSManagedObject 已失效

带有标签栏的 presentViewController

在 UINavigationController 中设置时图像不显示

UINavigationController - 何时释放推送的视图控制器等

使用 UINavigationController 从另一个控制器更新或重新加载 UIViewController?

为啥我似乎必须在 iOS 应用程序委托中键入 window.rootViewController?