Getting interactivePopGestureRecognizer dismiss callback/event

前端 未结 3 1592
[愿得一人]
[愿得一人] 2020-12-14 07:07

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

To be cl

相关标签:
3条回答
  • 2020-12-14 07:22

    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 - 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)")
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-14 07:29

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

    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.

    0 讨论(0)
  • 2020-12-14 07:37
    - (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
        }
    }
    
    
    0 讨论(0)
提交回复
热议问题