2015-09-18 49 views
0

我有這樣的習俗navigationController動畫我想要實現,但我想要的代碼被執行,只有當一個特定的viewController是在棧上。這裏是代碼:如何在自定義navigationController代碼中定位特定的viewController?

class ARNImageTransitionNavigationController: UINavigationController, UINavigationControllerDelegate { 

    weak var interactiveAnimator : ARNTransitionAnimator? 
    var currentOperation : UINavigationControllerOperation = .None 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     self.interactivePopGestureRecognizer?.enabled = false 
     self.delegate = self 
    } 

    func navigationController(navigationController: UINavigationController, 
     animationControllerForOperation operation: UINavigationControllerOperation, 
     fromViewController fromVC: UIViewController, 
     toViewController toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? 
    { 
     self.currentOperation = operation 

     if let _interactiveAnimator = self.interactiveAnimator { 
      return _interactiveAnimator 
     } 

     if operation == .Push { 
      return ARNImageZoomTransition.createAnimator(.Push, fromVC: fromVC, toVC: toVC) 
     } else if operation == .Pop { 
      return ARNImageZoomTransition.createAnimator(.Pop, fromVC: fromVC, toVC: toVC) 
     } 

     return nil 
    } 

    func navigationController(navigationController: UINavigationController, interactionControllerForAnimationController animationController: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning? { 
     if let _interactiveAnimator = self.interactiveAnimator { 
      if self.currentOperation == .Pop { 
       return _interactiveAnimator 
      } 
     } 
     return nil 
    } 
} 

現在我該怎麼添加這段代碼確保它實現了它的所有方法,但只有當特定的viewController在堆棧上時纔會執行它。

回答

1

如果你已經給有問題的視圖控制器提供了單獨的swift類,那麼你可以檢查他們的文件類名。假設您在推動時需要此動畫,例如導航堆棧的ProfilePageViewController,然後使用if operation == .Push && toVC.isKindOfClass(ProfilePageViewController)。我在您提供的代碼示例中添加了其餘代碼。

func navigationController(navigationController: UINavigationController, 
    animationControllerForOperation operation: UINavigationControllerOperation, 
    fromViewController fromVC: UIViewController, 
    toViewController toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? 
{ 
    self.currentOperation = operation 

    if let _interactiveAnimator = self.interactiveAnimator { 
     return _interactiveAnimator 
    } 

    if operation == .Push && toVC.isKindOfClass(swiftNameofVCClassFile) { 
     return ARNImageZoomTransition.createAnimator(.Push, fromVC: fromVC, toVC: toVC) 
    } else if operation == .Pop && fromVC.isKindOfClass(swiftNameofVCClassFile) { 
     return ARNImageZoomTransition.createAnimator(.Pop, fromVC: fromVC, toVC: toVC) 
    } 

    return nil 
} 
+0

這工作,謝謝 –

+0

,但我還有一個查詢,你會介意非常簡短地打這[聊天](http://chat.stackoverflow.com/rooms/90086/navigation-transition)嗎? –

0

此,我們用它來獲得從NavigationController特定視圖控制器代碼堆棧:

if let viewController: MyViewController = HGViewStask.findControllerForType() { 
    // Your code 
} 

不幸的是,這些功能:

class HGViewStack{ 
    static func findControllerForType<T>() -> T? { 
     let navController: UINavigationController = HGApp.sharedApplication().keyWindow?.rootViewController as! UINavigationController 
     let controller: [T] = navController.viewControllers.filter({ $0 is T }).map({$0 as! T}) 
     return controller.first 
    } 
} 

您可以在這裏定義一樣,您的所有功能,使用它會影響你所有的控制器,他們只是沒有效果,如果你想要的。

+0

因此,它會對viewController產生影響,我希望它能夠糾正?因爲多數民衆贊成在我想要的 –

+0

我認爲是的,你檢查天氣或不viewController是在堆棧上並獲得參考。 –

+0

,這樣有助於navigationController在堆棧中選擇特定的viewController,並執行給定的方法 –

相關問題