繁体   English   中英

iOS中是否有向后滑动手势的回调?

[英]Is there any callback for swipe back gesture in iOS?

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

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

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

那么向后滑动手势是否有任何回调?

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

interactivePopGestureRecognizer.enabled = NO;

最简单的方法是通过执行以下操作来挂钩已经内置到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:) ,否则它会在手势结束之前向你的选择器发送垃圾邮件。

试试这个。它为您提供了一些更好的解决方案。

- (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:)];
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM