Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

cthesky

macrumors member
Original poster
Aug 21, 2011
91
0
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. :)
 
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.
 
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:

Code:
- (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;
}
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.