UIButton in subview can't work once the UITapGestureRecognizer added in superview

Discussion in 'iOS Programming' started by cthesky, Sep 22, 2012.

  1. macrumors member

    Joined:
    Aug 21, 2011
    #1
    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. :)
     
  2. macrumors member

    Joined:
    Mar 27, 2012
    #2
    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.
     
  3. macrumors 6502

    Joined:
    Nov 5, 2010
    Location:
    Sea of Tranquility
    #3
    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;
    }
    
     

Share This Page