在iOS中,如何禁止工具栏中的手势识别?

发布于 2024-11-06 05:41:56 字数 820 浏览 4 评论 0原文

我的视图有一个工具栏,其中有两个 UIBarButtonItem 类型的按钮。我正在尝试实现一个手势识别器,这样当我点击视图中除工具栏之外的任何位置时,我都会调用一个选择器。点击工具栏中的栏按钮项应调用该按钮的操作方法。该视图符合 UIGestureRecognizerDelegate 协议并实现 shouldReceiveTouch 方法,如下所示:

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer 
                                                shouldReceiveTouch:(UITouch *) touch {
  // Disallow recognition of tap gestures in the toolbar
  if ((touch.view == self.sideOneBarButton.customView) &&
      (gestureRecognizer == self.tapRecognizer)) {
    return NO;
  }
  return YES;
}

问题是,当我点击工具栏按钮时,touch.view 的类型为 UTToolbarTextButton*,它是一个未记录的类,因此我的 if 语句失败,并且 shouldReceiveTouch 始终返回 YES,并且我的按钮事件永远不会被调用。

理想情况下,我想说:如果触摸位于工具栏中的任何位置,则返回 no。最好的方法是什么?

My view has a toolbar with two buttons of type UIBarButtonItem. I'm trying to implement a gesture recognizer such that when I tap anywhere in the view except in the toolbar, I call a selector. Tapping on a bar button item in the toolbar should call that button's action method. The view conforms to the UIGestureRecognizerDelegate protocol and implements the shouldReceiveTouch method as follows:

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer 
                                                shouldReceiveTouch:(UITouch *) touch {
  // Disallow recognition of tap gestures in the toolbar
  if ((touch.view == self.sideOneBarButton.customView) &&
      (gestureRecognizer == self.tapRecognizer)) {
    return NO;
  }
  return YES;
}

The problem is, when I tap on the toolbar button, touch.view is of type UTToolbarTextButton* which is an undocumented class, so my if statement fails and the shouldReceiveTouch always returns YES and my button event never gets called.

Ideally, I'd like to say: if the touch is anywhere in the toolbar, then return no. What's the best way to do this?

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

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

发布评论

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

评论(2

爱本泡沫多脆弱 2024-11-13 05:41:56

关于委托功能,这对我有好处。

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    if ([touch.view.superview isKindOfClass:[UIToolbar class]]) {
        return NO;
    }
    return YES;
}

About the delegate function, this is good for me.

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    if ([touch.view.superview isKindOfClass:[UIToolbar class]]) {
        return NO;
    }
    return YES;
}
他不在意 2024-11-13 05:41:56

有一种方法可以忽略任何不是您中心视图的手势。

例如,我有:

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
                               initWithTarget:self
                               action:@selector(dismissKeyboard)];

tap.delegate = self;

[self.view addGestureRecognizer:tap];

将手势委托添加到类中:

@interface myClass : UIViewController<..,UIGestureRecognizerDelegate,..>

现在是委托函数:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    if (touch.view == self.view)
    {
        return YES;
    }
    return  NO;
}

当我因为手势而无法使用 UIToolbar 时,这对我来说很有效

另外,正如 @LandedGently 提到的,这里有更多信息:

UIButton 位于具有 UITapGestureRecognizer 的视图内

因此人们不会不要错过评论中的链接。

There is a way to ignore the gesture on anything that isn't your central view.

For instance I have:

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
                               initWithTarget:self
                               action:@selector(dismissKeyboard)];

tap.delegate = self;

[self.view addGestureRecognizer:tap];

Add the gesture delegate to the class:

@interface myClass : UIViewController<..,UIGestureRecognizerDelegate,..>

and now the delegate function:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    if (touch.view == self.view)
    {
        return YES;
    }
    return  NO;
}

This has worked out for me when I couldn't use the UIToolbar because of the gesture

Also, as @LandedGently has mentioned, there is more information here:

UIButton inside a view that has a UITapGestureRecognizer

so people don't miss the link in the comments.

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