检测 UINavigationBar 中的滑动

发布于 2024-08-12 10:19:13 字数 75 浏览 6 评论 0原文

我试图让我的视图控制器检测由我的应用程序自动显示的 UINavigationBar 中的滑动,但它拒绝检测滑动。我有什么办法可以做到吗?

I am trying to get my view controller to detect swipes in the UINavigationBar that is automatically displayed by my app, but it refuses to detect swipes. Is there any way I can do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柠檬 2024-08-19 10:19:13

假设您想检测导航栏中向左的滑动,您可以在创建导航控制器时执行以下操作:

   UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(scrollViewSwipedLeft:)];
   [swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
   [self.navigationController.navigationBar addGestureRecognizer:swipeLeft];

然后创建一个如下所示的方法来处理它:

-(void) didSwipedLeft: (UISwipeGestureRecognizer *) gesture {

  if (gesture.state != UIGestureRecognizerStateEnded) {
      return;
  }

  //do something    
}

OBS:As您的导航控制器是一个将在应用程序生命周期的几个步骤中保持活动状态的类,请务必注意这一点,并仅在创建导航控制器时添加手势识别器(这意味着仅添加一次),以便您不要不断地堆积手势识别器,这不仅会导致内存泄漏,而且还可能使您的方法 didSwipedLeft 被多次调用。

Supposing you want to detect swipes to the left in your navigation bar, you could do something like this when you create the navigation controller:

   UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(scrollViewSwipedLeft:)];
   [swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
   [self.navigationController.navigationBar addGestureRecognizer:swipeLeft];

and then create a method like the one below to handle it:

-(void) didSwipedLeft: (UISwipeGestureRecognizer *) gesture {

  if (gesture.state != UIGestureRecognizerStateEnded) {
      return;
  }

  //do something    
}

OBS: As you navigation controller is a class that will remain alive for several steps of you application life cycle, it is important to pay attention to that and add the gesture recognizer only when you create the navigation controller (which means only add it once) so that you dont keep piling gestures recognizer one over each other, wich will lead not only to a memory leak, but also might make your method didSwipedLeft to be called more than once.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文