本文介绍了获取 interactivePopGestureRecognizer 解除回调/事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

interactivePopGestureRecognizer 解除(弹出)视图控制器上获取回调或事件是否有干净的解决方案?

Is there a clean solution on getting a callback or event on the view controller being dismissed (popped) by an interactivePopGestureRecognizer?

需要明确的是,在这个手势识别器弹出控制器之前,我需要在最顶层的控制器(而不是其他控制器)上调用一些显式方法.我不想在导航控制器上获取事件并将事件发送到适当的控制器,并且我不想使用 viewWillAppearviewWillDissapear...

To be clear I need some explicit method getting called on the top most controller (and no other) before the controller will be popped by this gesture recogniser. I do not want to get the event on the navigation controller and send the event to the appropriate controller and I do not want to use viewWillAppear or viewWillDissapear...

我最接近的事情是在手势中添加一个目标/选择器对,只有 2 个问题.首先,如果控制器将被解散,我无法获得直接信息(UIGestureRecognizerStateEnded 在任何情况下都会触发).第二次在控制器被解雇后,我需要从识别器中删除目标.

The closest thing I have is adding a target/selector pair to the gesture having only 2 problems. First I can't get a direct information if the controller will be dismissed or not (UIGestureRecognizerStateEnded will fire in any case). Second after the controller is dismissed I need to remove the target from the recogniser.

原因是我有一些控制器需要向他们的代表发送一些信息.使用完成"和取消"按钮触发事件,调用委托方法,然后弹出控制器.我需要尽可能减少对代码的更改.

The reason for this is I have a few controllers that need to send some information to their delegates. With having "done" and "cancel" buttons the event is triggered, delegate methods get called and then the controller is popped. I need pretty much the same to happen with as least changes to the code as possible.

此手势的另一种情况是引发警报视图并恢复操作的可能性:当此手势结束询问您确定要取消工作"并让用户选择时,是否有显示警报视图的方法如果控制器将被弹出或带回.

Another situation on this gesture is possibility of throwing an alert view and reverting the action: Is there a way of showing alert view when this gesture ends asking like "are you sure you wish to cancel your work" and have the user choose if the controller will be popped or brought back.

推荐答案

我知道这已经过时了,但对于可能面临类似问题的其他人来说.这是我使用的方法.首先,我将 UINavigationControllerDelegate 注册到我的导航控制器.委托需要实现.

I know this is old but for anyone else who might be facing similar problems. Here is the approach I used. First I register a UINavigationControllerDelegate to my navigation controller. The delegate needs to implement.

Objective-C

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated

斯威夫特

func navigationController(navigationController: UINavigationController, willShowViewController viewController: UIViewController, animated: Bool)

所以实现看起来像这样.

So the implementation would look something like this.

Objective-C

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
        id<UIViewControllerTransitionCoordinator> tc = navigationController.topViewController.transitionCoordinator;
        [tc notifyWhenInteractionEndsUsingBlock:^(id<UIViewControllerTransitionCoordinatorContext> context) {
            NSLog(@"Is cancelled: %i", [context isCancelled]);
    }];
}

斯威夫特

func navigationController(navigationController: UINavigationController, willShowViewController viewController: UIViewController, animated: Bool) {
    if let coordinator = navigationController.topViewController?.transitionCoordinator() {
        coordinator.notifyWhenInteractionEndsUsingBlock({ (context) in
            print("Is cancelled: (context.isCancelled())")
        })
    }
}

当用户抬起手指并且 (Objec-C)[context isCancelled]; (Swift)context.isCancelled() 将返回

code>YES/true 如果动画被反转(视图控制器没有弹出),否则 NO/false.context 中还有更多有用的东西,比如涉及的视图控制器和发布时完成的动画百分比等.

The callback will fire when the user lifts her finger and the (Objec-C)[context isCancelled]; (Swift)context.isCancelled() will return YES/true if the animation was reversed (the view controller was not popped), else NO/false. There is more stuff in the context that can be of use, like both view controllers involved and the percentage of the animation that was completed upon release etc.

这篇关于获取 interactivePopGestureRecognizer 解除回调/事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 08:12