Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I had a bit of a hard time for this problem. The accepted answer wasn't enough. No matter what I put in that method the pan or longpress handlers would get invoked. A solution I found was as follows:</p> <ol> <li>Ensure the gesture recognizers' delegates are assigned to the same class (in my case self) and ensure the delegate class is a <code>UIGestureRecognizerDelegate</code>.</li> <li><p>Add the following delegate method to your class (as per the answer above):</p> <pre><code>- (BOOL) gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { return YES; } </code></pre></li> <li><p>Add the following delegate method to your class:</p> <pre><code>- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer { if([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]] &amp;&amp; ! shouldAllowPan) { return NO; } return YES; } </code></pre></li> <li><p>Then add either a property or ivar which will track if the pan should be allowed to begin (see method above). In my case <code>BOOL shouldAllowPan</code>.</p></li> <li><p>Set the BOOL to <code>NO</code> in your <code>init</code> or <code>viewDidLoad</code>. Inside your longPress handler set the BOOL to <code>YES</code>. I do it like this: </p> <pre><code>- (void) longPressHandler: (UILongPressGestureRecognizer *) gesture { if(UIGestureRecognizerStateBegan == gesture.state) { shouldAllowPan = NO; } if(UIGestureRecognizerStateChanged == gesture.state) { shouldAllowPan = YES; } } </code></pre></li> <li><p>Inside the panHandler I do a check on the BOOL:</p> <pre><code>- (void)panHandler:(UIPanGestureRecognizer *)sender{ if(shouldAllowPan) { // do your stuff } </code></pre></li> <li><p>And finally reset the BOOL within the panHandler:</p> <pre><code>else if(sender.state == UIGestureRecognizerStateEnded || sender.state == UIGestureRecognizerStateFailed || sender.state == UIGestureRecognizerStateCancelled) { shouldAllowPan = NO; } </code></pre></li> <li><p>And then go grab a beer to congratulate yourself. ;)</p></li> </ol>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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