如何在自定义 navigationController 代码中定位特定的 viewController?

How do I target a specific viewController in a custom navigationController code?

我有这个要实现的自定义 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 在堆栈上时才实现?

这是我们用来从 NavigationController 堆栈中获取特定 ViewController 的代码:

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

您可以像这样在此处定义的所有函数中使用它:

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

不幸的是,这些功能会影响你所有的控制器,它们只是没有效果,如果你想要的话。

如果您已将个别 swift classes 提供给有问题的视图控制器,那么您可以检查它们的文件 class 名称。假设你在推动时想要这个动画,例如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
}