简体   繁体   English

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

[英]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.

相关问题 Flutter:iOS 带有数据的向后滑动手势 - Flutter: iOS swipe back gesture with data 如何在 iOS 上的 UINavigationController 中禁用向后滑动手势 7 - How to disable back swipe gesture in UINavigationController on iOS 7 iOS Web App:使用向后滑动手势返回 - iOS Web App: Use back swipe gesture to go back 使用ScrollableView在iOS上无法使用滑动后退手势 - Swipe back gesture doesn't work on iOS with ScrollableView 将UIPageViewController滑动与iOS 7 UINavigationController反滑动手势相结合 - Combine UIPageViewController swipes with iOS 7 UINavigationController back-swipe gesture 取消 iOS 7 向后滑动手势后,导航堆栈变得不可用 - Navigation stack becomes unusable after canceling iOS 7 back swipe gesture iOS 7+向后滑动手势与stateChange动画冲突 - iOS 7+ swipe back gesture conflict with stateChange animations 如何在iOS 7的UINavigationController中创建自定义向后滑动手势 - How to create custom back swipe gesture in UINavigationController on iOS 7 如何在iOS 8中的UINavigationController中禁用后退滑动手势? - How do disable back swipe gesture in UINavigationController in iOS 8? 如何将手势选择滑动到上一个View iOS? - How to swipe gesture segue back to previous view ios?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM