iOS 中有滑动返回手势的回调吗?

xi.*_*lin 5 objective-c uinavigationcontroller ios

有时,当用户返回到上一个时UIViewController,我想做点什么。

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

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

那么滑动返回手势有回调吗?

目前我只能通过以下方式在我的应用程序中禁用此类页面

interactivePopGestureRecognizer.enabled = NO;
Run Code Online (Sandbox Code Playgroud)

jak*_*ken 7

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

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