1

假設我有一個UINavigationController,我想在其上執行自定義轉換。爲了方便起見,我定義了一個過渡類:帶兩個代表的UINavigationController?

class WTPTransition: NSObject 
{ 
    // This is our Interaction Controller, which conforms to UIViewControllerInteractiveTransitioning. Apple's concrete class suits our needs. 
    var interactionController : UIPercentDrivenInteractiveTransition? 
} 

extension WTPTransition : UIViewControllerAnimatedTransitioning 
{ 
    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval { 
     return 1 
    } 

    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) { 
     let toView = transitionContext.viewController(forKey: .to)!.view! 
     let fromView = transitionContext.viewController(forKey: .from)!.view! 

     transitionContext.containerView.insertSubview(toView, belowSubview: fromView) 

     UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: { 
      fromView.alpha = 0.0 
     }, completion: { finished in 
      transitionContext.completeTransition(!transitionContext.transitionWasCancelled) 
     }) 
    } 
} 

然後我擴展它,這樣我可以簡單地設置一個UINavigationController的delegate財產,讓我轉換:

extension WTPTransition : UINavigationControllerDelegate 
{ 
    func navigationController(_ navigationController: UINavigationController, animationControllerFor operation: UINavigationControllerOperation, from fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? { 
     return self 
    } 

    func navigationController(_ navigationController: UINavigationController, interactionControllerFor animationController: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning? { 
     return interactionController 
    } 
} 

這工作得很好,但會發生什麼如果我想要另一個班級觀察相同的導航控制器的事件?我不能有兩個對象作爲它的委託,那麼使用什麼正確的模式?將觀察控制器的轉換邏輯與顯示或解散的東西分開似乎很自然。

回答

0

我認爲這是非常骯髒的架構。

  1. 動畫它是隻有在轉換處於活動狀態時才需要的對象。
  2. 你應該有能力改變一些viewControllers的動畫。

我認爲你可以創建委託類,它將創建出現和消失的動畫。