How to trigger UIButton with UITapGestureRecognizer


To trigger UIButton touch action within a view that has a UITapGestureRecognizer, you have to implement the the UIGestureRecognizerDelegate protocol and its method -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch.


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


// test if our control subview is on-screen


if (self.controlSubview.superview != nil) {


if ([touch.view isDescendantOfView:self.controlSubview]) {


// we touched our control surface


return NO; // ignore the touch


}


}


return YES;


 


// handle the touch


 


}

or


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


if ([touch.view isKindOfClass:[UIButton class]]){


return FALSE;


}


return TRUE;


}


 


  1. No comments yet.
(will not be published)
Submit Comment

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Subscribe to comments feed
  1. No trackbacks yet.

SetPageWidth