Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIButton touch event falls through to underlying view

I have created a small UIView which contains two UIButtons. The view responds to UITapGesture events. The Buttons are supposed to respond to TouchUpInside, however when I tap the buttons the responder is the underlying view and the tap gesture selector is triggered. Looking for advice or suggestions.

like image 803
Kyle Avatar asked Sep 15 '25 07:09

Kyle


2 Answers

You can modify the method that responds to the tap gesture in the orange view:

-(void) handleTapFrom:(UITapGestureRecognizer*) recognizer {
    CGPoint location = [recognizer locationInView:orangeView];
    UIView *hitView = [orangeView hitTest:location withEvent:nil];

    if ([hitView isKindOfClass:[UIButton class]]) {
        return;
    }

    //code that handle orange view tap
    ...
}

This way if you touch a UIButton, the tap will be ignored by the underlying view.

like image 129
Riccardo Marotti Avatar answered Sep 17 '25 00:09

Riccardo Marotti


The right answer (which prevents the tabrecognizer from highjacking any taps and doesn't need you to implement a delegate etc) is found here. I got a lead to this answer via this post.

In short use:

tapRecognizer.cancelsTouchesInView = NO;

"Which prevents the tap recognizer to be the only one to catch all the taps"

like image 21
EeKay Avatar answered Sep 17 '25 00:09

EeKay