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

时间:2015-09-18 07:07:00

标签: ios swift uiviewcontroller uinavigationcontroller

我想实现这个自定义的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在堆栈中时才会实现?

2 个答案:

答案 0 :(得分: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
}

答案 1 :(得分:0)

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

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
    }
}

您可以在此处定义的所有功能中使用它:

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

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