Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIButton not working when single tap gesture is added to superview

I have a UIButton set up and hooked up to an action in my view controller as usual. Just this by itself works fine.

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?

like image 917
Andrew Avatar asked Jan 10 '11 03:01

Andrew


3 Answers

Your problem is similar to this question which hopefully has been answered ;)

UIButton inside a view that has a UITapGestureRecognizer

like image 187
vdaubry Avatar answered Nov 11 '22 22:11

vdaubry


Here is a simple solution

singleTapGesture.cancelsTouchesInView = NO;
like image 45
ragnarius Avatar answered Nov 11 '22 21:11

ragnarius


Andrew,

It seems to me that you want to call a function when the user taps any where except the UI objects.

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.

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.

Its simpler than what you were attempting in your code

like image 1
Robin Avatar answered Nov 11 '22 23:11

Robin