ios - 带有自定义动画的 UINavigationViewControllerDelegate 会破坏默认行为吗?

标签 ios uinavigationcontroller delegates

我一直在尝试让 UINavigationViewControllerDelegate 实现自定义转换所需的方法。它们按预期工作,我也能够将交互式过渡添加到混音中。

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

文档指出应该从 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;
}

这里还有什么问题?

最佳答案

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

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

    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
    }

关于ios - 带有自定义动画的 UINavigationViewControllerDelegate 会破坏默认行为吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24135053/

相关文章:

ios - 无法在 swift 4 iOS 的文档目录中保存正确的 firebase 图像

ios - 如何以某种通用方式删除有序的对多 CoreData 关系中的对象?

ios - 自定义segue导致导航项消失

IOS - 替换 NavController 中的初始 View

ios - 如果我们在其 UIScrollView 中包含每个 UIScrollViewDelegate 方法会怎样?

c# - 委托(delegate)字典模式的默认值

objective-c - iOS - NSString 到 uint

ios - 相当于 loginViewShowingLoggedInUser

swift - 未触发刷新控件目标操作

objective-c - OCUnit 测试 Objective-C 中的协议(protocol)/回调/委托(delegate)