在iOS中,如何禁止工具栏中的手势识别?
我的视图有一个工具栏,其中有两个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
关于委托功能,这对我有好处。
About the delegate function, this is good for me.
有一种方法可以忽略任何不是您中心视图的手势。
例如,我有:
将手势委托添加到类中:
现在是委托函数:
当我因为手势而无法使用 UIToolbar 时,这对我来说很有效
另外,正如 @LandedGently 提到的,这里有更多信息:
UIButton 位于具有 UITapGestureRecognizer 的视图内,
因此人们不会不要错过评论中的链接。
There is a way to ignore the gesture on anything that isn't your central view.
For instance I have:
Add the gesture delegate to the class:
and now the delegate function:
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.