UINavigationViewControllerDelegate与自定义动画打破默认行为?

时间:2014-06-10 07:17:11

标签: ios uinavigationcontroller delegates

我一直在努力让UINavigationViewControllerDelegate实现自定义转换所需的方法。它们按预期工作,我也可以将混合转换添加到组合中。

问题在于,当我实现这些方法时,我完全失去了正常导航过渡的默认“向右滑动以返回”支持。我在进入视图控制器之前设置navigationController.delegate = nil以获得正常的转换。这意味着我必须存储实际的旧委托,并在从视图返回时重新设置它。

文档说明应该从nilnavigationController:interactionControllerForAnimationController:返回navigationController:animationControllerForOperation:fromViewController:toViewController:,这正是我正在做的事情:

- (id<UIViewControllerAnimatedTransitioning>)navigationController:
        (UINavigationController *)navigationController 
    animationControllerForOperation:(UINavigationControllerOperation)operation 
    fromViewController:(UIViewController *)fromVC 
    toViewController:(UIViewController *)toVC
{
    if([fromVC isKindOfClass:[MainViewController class]] &&
       [toVC isKindOfClass:[MenuViewController class]]) {
        self.menuTransition.isPresentation = YES;
        return self.menuTransition;
    } else if([toVC isKindOfClass:[MainViewController class]] &&
              [fromVC isKindOfClass:[MenuViewController class]]){
        self.menuTransition.isPresentation = NO;
        return self.menuTransition;
    }
    return nil;
}

- (id<UIViewControllerInteractiveTransitioning>) navigationController 
        (UINavigationController *)navigationController 
    interactionControllerForAnimationController:
        (id<UIViewControllerAnimatedTransitioning>)animationController
{
    MenuTransition *t = (MenuTransition*)animationController;

    if(![t isPresentation] && [t isInteractive]) {
        return self.menuTransition;
    }
    return nil;
}

这里还有什么可能是错的?

2 个答案:

答案 0 :(得分:2)

文档确实给人的印象是返回nil会起作用,但我发现手势识别器是冲突的。实现gestureRecognizerShouldBegin为我解决了这个问题。

*注意,这是用swift编写的,但应该很容易转换为obj-c。 这是一个带有UIGestureRecognizerDelegate协议的导航控制器子类

    override func viewDidLoad() {
        super.viewDidLoad()
        self.delegate = self
        self.interactivePopGestureRecognizer.delegate = self

        self.transitionGesture = UIPanGestureRecognizer(target: self, action: "handlePanGesture:")
        self.view.addGestureRecognizer(transitionGesture)
        self.transitionGesture!.delegate = self

    func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer!) -> Bool {
        if  self.transitionCoordinator()?.isAnimated() {
            return false
        }


        if self.viewControllers.count < 2 {
            return false
        }

        var currentVC: UIViewController? = self.viewControllers[self.viewControllers.count-1] as? UIViewController
        if let vc = currentVC as? MyCustomVC {
            if gestureRecognizer == self.transitionGesture {
                return true
            }
        } else if gestureRecognizer == self.interactivePopGestureRecognizer {
            return true
        }
        return false
    }

答案 1 :(得分:0)

当在viewController1中将viewController2推入navigationController.delegate = nil时,则在您推入的视图控制器中,交互弹出手势将是默认的,并且可以完美工作,并且当您弹出viewController2时 将此代码添加到viewController1

override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        navigationController?.delegate = navigationController as? UINavigationControllerDelegate
}