获取interactivePopGestureRecognizer会解除回调/事件

Mat*_*lak 23 iphone objective-c uinavigationcontroller ios ios7

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

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

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

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

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

Pet*_*lom 50

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

Objective-C的

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

迅速

func navigationController(navigationController: UINavigationController, willShowViewController viewController: UIViewController, animated: Bool)
Run Code Online (Sandbox Code Playgroud)

所以实现看起来像这样.

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]);
    }];
}
Run Code Online (Sandbox Code Playgroud)

迅速

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

当用户抬起手指时,回调将触发,并且(Objec-C)[context isCancelled];(Swift)context.isCancelled()将返回YES/ true如果动画被反转(视图控制器未弹出),否则NO/ false.有一个在更多的东西context,可以是有用的,如涉及两个视图控制器,并已达成释放等完成了动画的百分比

  • 我不得不做一些修改... id <UIViewControllerTransitionCoordinator> tc = navigationController.topViewController.transitionCoordinator; [tc notifyWhenInteractionEndsUsingBlock:^(id <UIViewControllerTransitionCoordinatorContext> context){UIViewController*topController = [context viewControllerForKey:UITransitionContextFromViewControllerKey]; if([topController respondsToSelector:@selector(controllerDismissedByPopGesture :)]){[topController performSelector:@selector(controllerDismissedByPopGesture :) withObject:@([context isCancelled])]; }}; (3认同)

Ted*_*Ted 11

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)")
        })
    }
}
Run Code Online (Sandbox Code Playgroud)

Swift 4-5.1 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)")
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 这个答案非常有效!谢谢。在 Swift 5.1 / iOS 13.1 中测试 (2认同)