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

twelveoclock

macrumors newbie
Original poster
Aug 2, 2013
1
0
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.
 
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.



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