【问题标题】:UINavigationViewControllerDelegate with custom animations break default behavior?带有自定义动画的 UINavigationViewControllerDelegate 会破坏默认行为?
【发布时间】:2014-06-10 07:17:11
【问题描述】:

我一直在尝试使用UINavigationViewControllerDelegate 来实现自定义转换所需的方法。它们按预期工作,我也可以在混音中添加交互式过渡。

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

文档指出应该从navigationController:interactionControllerForAnimationController:navigationController:animationControllerForOperation:fromViewController:toViewController: 返回nil,这正是我正在做的:

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

这里还有什么问题?

【问题讨论】:

    标签: ios uinavigationcontroller delegates


    【解决方案1】:

    文档确实给人的印象是返回 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
        }
    

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 2020-12-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-10-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多