获取interactivePopGestureRecognizer会解除回调/事件

时间:2013-12-17 16:06:57

标签: ios iphone objective-c ios7 uinavigationcontroller

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

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

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

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

这种姿势的另一种情况是投掷警报视图并恢复动作的可能性:当这个姿势结束时,是否有一种显示警报视图的方式,例如"你确定要取消你的工作吗?并让用户选择是否弹出或带回控制器。

3 个答案:

答案 0 :(得分:44)

我知道这是旧的,但对于可能面临类似问题的其他人来说。这是我使用的方法。首先,我将UINavigationControllerDelegate注册到我的导航控制器。代表需要实施。

<强>目标C

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

<强>夫特

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

所以实现看起来像这样。

<强>目标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()将返回YES / true反转(视图控制器未弹出),否则NO / falsecontext中有更多可以使用的东西,比如所涉及的视图控制器和在发布时完成的动画的百分比等。

答案 1 :(得分:5)

Swift 4 iOS 7-10

func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {

    if let coordinator = navigationController.topViewController?.transitionCoordinator {
        coordinator.notifyWhenInteractionEnds({ (context) in
            print("Is cancelled: \(context.isCancelled)")
        })
    }
}

Swift 4 iOS 10 +

func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {

    if let coordinator = navigationController.topViewController?.transitionCoordinator {
        coordinator.notifyWhenInteractionChanges { (context) in
            print("Is cancelled: \(context.isCancelled)")
        }
    }
}

答案 2 :(得分:-1)

<: Vector
相关问题