[英]Is there any callback for swipe back gesture in iOS?
Sometimes when user go back to the previous UIViewController
, I want to do something.有时当用户回到之前的
UIViewController
,我想做点什么。
If the user clicked the back button in UINavigationBar
, I can capture the event.如果用户单击
UINavigationBar
的后退按钮,我可以捕获该事件。
But if they use the swipe back gesture to go back, I cannot respond to the change.但是如果他们使用向后滑动的手势返回,我无法响应更改。
So is there any callback for swipe back gesture?那么向后滑动手势是否有任何回调?
Currently I can only disable this kind of page in my app through目前我只能通过以下方式在我的应用程序中禁用此类页面
interactivePopGestureRecognizer.enabled = NO;
The easiest way is to hook into the one that's already built into UINavigationController
by doing something like this:最简单的方法是通过执行以下操作来挂钩已经内置到
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
}
The remember to call removeTarget(_:action:)
in your selector, otherwise it'll spam your selector until the gesture ends.记得在你的选择器中调用
removeTarget(_:action:)
,否则它会在手势结束之前向你的选择器发送垃圾邮件。
Try this.It gives you some what better solution.试试这个。它为您提供了一些更好的解决方案。
- (void)viewDidLoad {
[super viewDidLoad];
UIScreenEdgePanGestureRecognizer *gesture = (UIScreenEdgePanGestureRecognizer*)[self.navigationController.view.gestureRecognizers objectAtIndex:0];
[gesture addTarget:self action:@selector(moved:)];
}
In Target method.在目标方法中。
-(void)moved:(id)sender{
// do what you want
//finally remove the target
[[self.navigationController.view.gestureRecognizers objectAtIndex:0] removeTarget:self action:@selector(moved:)];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.