Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Button Tap Event Get Overridden by Tap Gesture Recognizer?

I have a button that I want to make disappear if anything is tapped other than the button. So I set the target:action: for the delete button:

[self.deleteButton addTarget:self action:@selector(deleteButtonTapped:) forControlEvents:UIControlEventTouchUpInside];

followed later by adding the tap gesture recognizer to the containing view:

[self.superview addGestureRecognizer:self.tapOutsideDelete];

When I do this, the action for deleteButton is not executed, as if the button tap is not recognized. The gesture recognizer does work in this case. But it also works when I tap deleteButton, which leads me to think that the tap gesture recognizer has priority over the button tap.

If I remove the gesture recognizer, deleteButton works correctly.

I clearly don't understand how to handle these two events together. What do I need to do?

(deleteButton is mimicking the delete button of a table view cell, but in this case I have it in a header view. I expect to call a method to make the delete button to disappear when I tap anywhere in the table except the button itself, the same way it works in a cell.)

like image 367
Jim Avatar asked Jun 22 '12 22:06

Jim


1 Answers

Yes, the tap gesture gets first crack at the tap. You need to implement the gestureRecognizer:shouldReceiveTouch: method:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer 
            shouldReceiveTouch:(UITouch *)touch {

    if (touch.view == self.deleteButton) {
        return NO;
    }
    return YES;
}
like image 166
MusiGenesis Avatar answered Oct 30 '22 18:10

MusiGenesis