I have a UIButton set up and hooked up to an action in my view controller as usual. Just this by itself works fine.
我有一个UIButton设置并像往常一样连接到我的视图控制器中的动作。只是这个本身工作正常。
Now, I have added the following to my view controller to set up a single tap:
现在,我已将以下内容添加到我的视图控制器中以设置单击:
- (void)viewDidLoad {
[super viewDidLoad];
UITapGestureRecognizer * singleTapGesture = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:@selector(handleSingleTap:)];
[self.view addGestureRecognizer:singleTapGesture];
[singleTapGesture release]; }
Now, every time I tap the button it looks like it was tapped but it fires the gesture recognizer code instead of the button action.
现在,每次点击按钮时,它看起来都被点击了,但它会触发手势识别器代码而不是按钮动作。
How can I make it so that the button works when I tap the button and the tap gesture works when I tap anywhere else in the view?
当我点击按钮时,我如何才能使按钮工作,当我点击视图中的任何其他位置时,点按手势可以正常工作?
3 个解决方案
#1
13
Your problem is similar to this question which hopefully has been answered ;)
你的问题类似于这个问题,希望得到回答;)
UIButton inside a view that has a UITapGestureRecognizer
UIButton在具有UITapGestureRecognizer的视图中
#2
10
Here is a simple solution
这是一个简单的解决方案
singleTapGesture.cancelsTouchesInView = NO;
#3
1
Andrew,
安德鲁,
It seems to me that you want to call a function when the user taps any where except the UI objects.
在我看来,当用户点击除UI对象之外的任何地方时,你想要调用一个函数。
so my suggestion is that create a custom button of the size of screen(320x460 assuming that the grey bar is shown) place it back of all the objects in the IB. By this time you will be able to see a UIButton with almost 0% opacity.
所以我的建议是创建一个屏幕大小的自定义按钮(假设显示灰色条,则为320x460)将其放回IB中的所有对象。到这个时候,您将能够看到几乎0%不透明度的UIButton。
and from the IB drag the selector option of the custom button to the file owners and select the function that you want to call and you are all done.
从IB拖动自定义按钮的选择器选项到文件所有者,并选择您要调用的功能,你就完成了。
Its simpler than what you were attempting in your code
它比您在代码中尝试的更简单
#1
13
Your problem is similar to this question which hopefully has been answered ;)
你的问题类似于这个问题,希望得到回答;)
UIButton inside a view that has a UITapGestureRecognizer
UIButton在具有UITapGestureRecognizer的视图中
#2
10
Here is a simple solution
这是一个简单的解决方案
singleTapGesture.cancelsTouchesInView = NO;
#3
1
Andrew,
安德鲁,
It seems to me that you want to call a function when the user taps any where except the UI objects.
在我看来,当用户点击除UI对象之外的任何地方时,你想要调用一个函数。
so my suggestion is that create a custom button of the size of screen(320x460 assuming that the grey bar is shown) place it back of all the objects in the IB. By this time you will be able to see a UIButton with almost 0% opacity.
所以我的建议是创建一个屏幕大小的自定义按钮(假设显示灰色条,则为320x460)将其放回IB中的所有对象。到这个时候,您将能够看到几乎0%不透明度的UIButton。
and from the IB drag the selector option of the custom button to the file owners and select the function that you want to call and you are all done.
从IB拖动自定义按钮的选择器选项到文件所有者,并选择您要调用的功能,你就完成了。
Its simpler than what you were attempting in your code
它比您在代码中尝试的更简单