Gesture recognizer interferes with UIButton

Discussion in 'iOS Programming' started by twelveoclock, Aug 2, 2013.

  1. twelveoclock macrumors newbie

    Joined:
    Aug 2, 2013
    #1
    I have a gesture recognizer that is interfering with `UIButton`s being pushed. I found a thread at http://stackoverflow.com/questions/3344341/uibutton-inside-a-view-that-has-a-uitapgesturerecognizer which suggested two solutions. One solution was to add `tap.cancelsTouchesInView = false;` to my code but this did nothing. Someone on that thread said that this only works when the view that contains the gesture is a sibling of a `UIControl`. My gesture is in a class called `ChessBoard` that is a subview of `UIView`. A `UILabel` is a subview of `ChessBoard` and the `UIButton` is a subview of that `UILabel`. So the view is a grandparent of the `UIControl` so this solution will not work.

    The other solution is to add code like the following:

    Code:
        - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
                if ([touch.view isKindOfClass:[UIControl class]]) {
                    // we touched a button, slider, or other UIControl
                    return NO; // ignore the touch
                }
            return YES; // handle the touch
        }
    
    I added the following code to my program in the `ChessBoard` class:

    Code:
        - (BOOL)gestureRecognizer:(UIGestureRecognizer*)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
            NSLog(@"%@", NSStringFromClass([touch.view class]));
            if([touch.view isKindOfClass:[UIButton class]]) {
                return false;
            }
            else {
                return true;
            }
        }
    
    However, it still does not work. The `NSLog` always says that `touch.view` is `ChessBoard`, even when the `UIButton` is being pushed. I don't understand is how the program determines what `touch.view' is.
     
  2. Duncan C macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #2
    I've wrestled with this as well. It's been a while, so my memory of how I solved it is a bit vague.

    If I remember correctly, what worked for me was to make the button not a subview of the view that has the gesture recognizer, but make it a sibling that is on top of the gesture recognizer view.



     

Share This Page