获取interactivePopGestureRecognizer会解除回调/事件

问题描述 投票:19回答:3

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

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

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

原因是我有一些控制器需要向他们的代表发送一些信息。通过“完成”和“取消”按钮触发事件,调用委托方法,然后弹出控制器。尽管对代码的更改尽可能少,但我需要几乎相同。

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

ios iphone objective-c ios7 uinavigationcontroller
3个回答
46
投票

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

Objective-C的

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

迅速

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

所以实现看起来像这样。

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


6
投票

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)")
        }
    }
}

-1
投票
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {

    void (^popInteractionEndBlock)(id<UIViewControllerTransitionCoordinatorContext>) =
    ^(id<UIViewControllerTransitionCoordinatorContext> context){
        if (context.completionVelocity >= 1.0f) {
            // complete
        }
    };
    if (@available(iOS 10,*)) {
        [self.navigationController.transitionCoordinator notifyWhenInteractionChangesUsingBlock:popInteractionEndBlock];
    } else {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
        [self.navigationController.transitionCoordinator notifyWhenInteractionEndsUsingBlock:popInteractionEndBlock];
#pragma GCC diagnostic pop
    }
}

© www.soinside.com 2019 - 2024. All rights reserved.