Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
393 views
in Technique[技术] by (71.8m points)

ios - Getting interactivePopGestureRecognizer dismiss callback/event

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

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...

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...