【问题标题】:Is there any callback for swipe back gesture in iOS?在 iOS 中是否有回扫手势的回调?
【发布时间】:2015-08-14 08:35:01
【问题描述】:

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

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

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

那么回扫手势有回调吗?

目前我只能通过

在我的应用中禁用这种页面
interactivePopGestureRecognizer.enabled = NO;

【问题讨论】:

  • viewWillDissapear / viewDidDisappear 没有做你想做的事吗?
  • @Tander 还有其他操作,例如 push new UIViewController,会导致 viewWillDisappear。我只是想钩住后面的动作。

标签: ios objective-c uinavigationcontroller


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    试试这个。它会给你一些更好的解决方案。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-06
      • 2012-10-17
      • 2022-10-14
      • 2019-09-21
      • 1970-01-01
      • 2013-10-30
      • 1970-01-01
      • 2018-08-25
      相关资源
      最近更新 更多