PDA

View Full Version : UIButton in subview can't work once the UITapGestureRecognizer added in superview




cthesky
Sep 23, 2012, 12:50 AM
Hi all,

In my application, I added UITapGestureRecognizer in a view (my app's root view). Once tap it, another new view will be added to this superview. In this new view, it consists a button. But what happened is this button cannot work properly, it cannot call its corresponding selector once I click (Touch up inside) it. Anyone know why this happened?

So, is it need to remove the UITapGestureRecognizer I added once I tap the superview and add another new view? Anyone has any suggestions?

Any comments and suggestions are welcome. Thanks a lot. :-)



Ides
Sep 23, 2012, 12:52 PM
I think that UIGestureReconizers sometimes "steal" touches from buttons and other objects. At least, I've had the same problem as you have with a button not working because I added a gesture recognizer to its super view. I don't actually know a whole lot about them though, maybe you should read up (http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIGestureRecognizer_Class/Reference/Reference.html).

CodeBreaker
Sep 30, 2012, 02:41 AM
Hi all,

In my application, I added UITapGestureRecognizer in a view (my app's root view). Once tap it, another new view will be added to this superview. In this new view, it consists a button. But what happened is this button cannot work properly, it cannot call its corresponding selector once I click (Touch up inside) it. Anyone know why this happened?

So, is it need to remove the UITapGestureRecognizer I added once I tap the superview and add another new view? Anyone has any suggestions?

Any comments and suggestions are welcome. Thanks a lot. :-)

The gesture recogniser will steal touches from your button. You can either disable the gesture recogniser from your view once you present your subview or you can use it's delegate methods to tell it not to receive touches on your button like this:


- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
CGPoint theTouchPoint = [touch locationInView:self];
CGRect buttonRect = [mySubViewWithTheButton convertRect:myButton.frame toView:self.view];
if (CGRectContainsPoint(buttonRect, theTouchPoint)) {
return NO;
}
return YES;
}