获取interactivePopGestureRecognizer会关闭回调/事件 [英] Getting interactivePopGestureRecognizer dismiss callback/event

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

问题描述

是否有一个干净的解决方案可以通过 interactivePopGestureRecognizer 来解除(弹出)视图控制器上的回调或事件?

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

要明确我需要在最高控制器(而不是其他控制器)上调用一些显式方法,然后才能通过此手势识别器弹出控制器。我不想在导航控制器上获取事件并将事件发送到适当的控制器,我不想使用 viewWillAppear viewWillDissapear ...

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

Swift

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]);
    }];
}

Swift

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)<$ c $时,回拨将会触发c> [context isCancelled]; (Swift) context.isCancelled()将返回 YES / true 如果动画被反转(视图控制器没有弹出),否则 NO / 上下文中有更多东西可供使用,例如所涉及的视图控制器和发布时完成的动画的百分比等。

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会关闭回调/事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆