Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>For that you need to create you own custom gesture recognizer. The best way would be for you to subclass <code>UILongPressGestureRecognizer</code> and make it "accept" the long press only after the swipe has ended. E.g., in the <code>touchesBegan</code> method </p> <pre><code>- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event { &lt;if swipe recognizer action method has been called&gt; [super touchesBegan:touches withEvent:event]; } } </code></pre> <p>In this way, both gesture recognizers would try to recognize the gesture: the swipe recognizer would do so immediately; while the custom recognizer would "wait" for the swipe recognizer to have fired.</p> <p>An easy way to implement the condition <code>&lt;if swipe recognizer action method has been called&gt;</code> would be setting a global flag in your swipe action (you set it when the swipe action is executed; then the custom recognizer reads its value). This is <em>easy</em>, by far not the best implementation you can find.</p> <p>Another approach would be relying on the <code>requiresGestureRecognizerToFail</code> to chain 3 standard gesture recognizers, let's call them A, B, and C, where:</p> <ol> <li>A is a <code>UISwipeGestureRecognizer</code>;</li> <li>C is a <code>UILongPressGestureRecognizer</code>;</li> <li>B is any gesture recognizer.</li> </ol> <p>You configure them like this:</p> <pre><code>C --&gt; B --&gt; A </code></pre> <p>whereby <code>x --&gt; y</code> denotes that <code>x</code> requires <code>y</code> to fail. Thus, you would have that <code>C</code> (your long press gesture recognizer) will require the <code>B</code> GR to fail and <code>B</code> requires <code>A</code> (your swipe recognizer) to fail; <code>B</code> will fail as soon as <code>A</code> will recognized the swipe; once <code>B</code> fails, <code>C</code> will be allowed to recognize the long press if any.</p> <p>EDIT:</p> <p>after reading your comment, would you mind trying this and see if it helps out:</p> <ol> <li><p>remove your overridden <code>touchesBegan</code> and replace it by:</p> <pre><code>- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event { } </code></pre></li> <li><p>then define:</p> <pre><code>- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event { if (vc.methodHasBeenCalled) { [super touchesBegan:touches withEvent:event]; } else { return; } } </code></pre></li> </ol> <p>If this does not work, then make your custom gesture recognizer inherit from a generic gesture recognizer and </p> <ol> <li><p>leave <code>touchesBegan:</code> in place and replace <code>touchesMoved</code> with:</p> <pre><code>- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event { self.lastTouch = [touches anyObject]; if (vc.methodHasBeenCalled &amp;&amp; self.gestureNotBegunYet == YES) { self.gestureNotBegunYet = NO; [self performSelector:@selector(recognizeLongPress) withObject:nil afterDelay:1.0]; } else { return; } } </code></pre></li> </ol> <p>and add:</p> <pre><code>- (float)travelledDistance { CGPoint currentLocation = [self.lastTouch locationInView:self.view.superview]; return sqrt(pow((currentLocation.x - self.initialLocation.x), 2.0) + pow((currentLocation.y - self.initialLocation.y), 2.0)); } - (void)fail { self.gestureNotBegunYet = YES; [NSObject cancelPreviousPerformRequestsWithTarget:self]; } - (void)recognizeLongPress { if ([self travelledDistance] &lt; kTapDragThreshold) { self.longPressed = YES; self.state = UIGestureRecognizerStateChanged; } else { [self fail]; self.state = UIGestureRecognizerStateFailed; } } - (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event { [self fail]; [super touchesEnded:touches withEvent:event]; } - (void)touchesCancelled:(NSSet*)touches withEvent:(UIEvent*)event { [self fail]; [super touchesCancelled:touches withEvent:event]; } </code></pre> <p>You will need to define in <code>.h</code></p> <pre><code>@property(nonatomic) CGPoint initialLocation; @property(nonatomic, retain) UITouch* lastTouch; @property(nonatomic) BOOL gestureNotBegunYet; </code></pre>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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