iOS有没有回滑手势的回调?

Is there any callback for swipe back gesture in iOS?

有时当用户回到之前的 UIViewController 时,我想做点什么。

如果用户在 UINavigationBar 中点击了后退按钮,我可以捕获该事件。

但如果他们使用滑动返回手势返回,我无法响应更改。

那么回滑手势有回调吗?

目前我只能通过

在我的应用中禁用此类页面
interactivePopGestureRecognizer.enabled = NO;

试试 this.It 给你一些更好的解决方案。

- (void)viewDidLoad {
    [super viewDidLoad];
    UIScreenEdgePanGestureRecognizer *gesture = (UIScreenEdgePanGestureRecognizer*)[self.navigationController.view.gestureRecognizers objectAtIndex:0];
        [gesture addTarget:self action:@selector(moved:)];
}

在目标方法中。

-(void)moved:(id)sender{    
    // do what you want

//finally remove the target
    [[self.navigationController.view.gestureRecognizers objectAtIndex:0] removeTarget:self action:@selector(moved:)];
}

最简单的方法是连接到 UINavigationController 中已经内置的方法,方法如下:

override func viewDidLoad() {
  super.viewDidLoad()
  navigationController?.interactivePopGestureRecognizer?.addTarget(self, action: #selector(MyViewController.handleBackswipe))
}
    
@objc private func handleBackswipe() {
    navigationController?.interactivePopGestureRecognizer?.removeTarget(self, action: #selector(MyViewController.handleBackswipe))
    // insert your custom code here
}

记得在您的选择器中调用 removeTarget(_:action:),否则它会向您的选择器发送垃圾邮件,直到手势结束。