Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Set a delegate on the recognizer and implement <code>gestureRecognizer:shouldReceiveTouch:</code>.</p> <p>The implementation should basically block touches on subviews, but there will probably be some additional criteria according to your actual view hierarchy and setup.</p> <p>The sample below simply checks if the hit view is a direct subview and if it has any gesture recognizers, in which case it is allowed to see the touches.</p> <p>Blocking touches should be more robust than fiddling with recognizer states, as there is no chance for any unwanted views to fire their recognizers. The requirement of implementing custom criteria is a drawback, but again I think it's more robust to explicitly implement behavior when working around a bug of unknown cause like in this case.</p> <pre><code>#import "View1234.h" @interface View1234 () &lt;UIGestureRecognizerDelegate&gt; @end @implementation View1234 - (id) initWithId:(NSString *)vid_ { if (self = [super init]) { vid = [vid_ retain]; UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] init]; tapGesture.numberOfTapsRequired = 1; tapGesture.numberOfTouchesRequired = 1; [tapGesture addTarget:self action:@selector(handleTap:)]; tapGesture.delegate = self; [self addGestureRecognizer:tapGesture]; [tapGesture release]; } return self; } //- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{ // // if ([gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]]) { // if (self.tag==gestureRecognizer.view.tag) { // return YES; // } // } // // return NO; //} - (void) handleTap:(id)tap { NSLog(@"tap %@", vid); } - (void) dealloc { [vid release]; vid = nil; [super dealloc]; } - (BOOL)shouldReceiveTouchOnView:(UIView *)hitView { NSLog(@"Should view:\n%@\nreceive touch on view:\n%@", self, hitView); // Replace this implementation with whatever you need... // Here, we simply check if the view has a gesture recognizer and // is a direct subview. BOOL res = (hitView.gestureRecognizers.count == 0 &amp;&amp; [self.subviews containsObject:hitView]); NSLog(@"%@", res? @"YES":@"NO"); return res; } #pragma mark - Gesture Recognizer Delegate - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch { UIView *hitView = [self hitTest:[touch locationInView:self.superview] withEvent:nil]; if (hitView == self) { NSLog(@"Touch not in subview"); return YES; } return [self shouldReceiveTouchOnView:hitView]; } @end </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload