Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here is a simple custom gesture recognizers for double taps where you can specify the maximum allowed time between taps. This is based on @Walters answer.</p> <p>PbDoubleTapGestureRecognizer.h :</p> <pre><code>@interface PbDoubleTapGestureRecognizer : UIGestureRecognizer @property (nonatomic) NSTimeInterval maximumDoubleTapDuration; @end </code></pre> <p>PbDoubleTapGestureRecognizer.m :</p> <pre><code>#import "PbDoubleTapGestureRecognizer.h" #import &lt;UIKit/UIGestureRecognizerSubclass.h&gt; @interface PbDoubleTapGestureRecognizer () @property (nonatomic) int tapCount; @property (nonatomic) NSTimeInterval startTimestamp; @end @implementation PbDoubleTapGestureRecognizer - (id)initWithTarget:(id)target action:(SEL)action { self = [super initWithTarget:target action:action]; if (self) { _maximumDoubleTapDuration = 0.3f; // assign default value } return self; } -(void)dealloc { [NSObject cancelPreviousPerformRequestsWithTarget:self]; } - (void)reset { [super reset]; [NSObject cancelPreviousPerformRequestsWithTarget:self]; self.tapCount = 0; self.startTimestamp = 0.f; } - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [super touchesBegan:touches withEvent:event]; if (touches.count != 1 ) { self.state = UIGestureRecognizerStateFailed; } else { if (self.tapCount == 0) { self.startTimestamp = event.timestamp; [self performSelector:@selector(timeoutMethod) withObject:self afterDelay:self.maximumDoubleTapDuration]; } self.tapCount++; } } - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { [super touchesMoved:touches withEvent:event]; } - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { [super touchesEnded:touches withEvent:event]; if (self.tapCount &gt; 2) { self.state = UIGestureRecognizerStateFailed; } else if (self.tapCount == 2 &amp;&amp; event.timestamp &lt; self.startTimestamp + self.maximumDoubleTapDuration) { [NSObject cancelPreviousPerformRequestsWithTarget:self]; NSLog(@"Recognized in %f", event.timestamp - self.startTimestamp); self.state = UIGestureRecognizerStateRecognized; } } - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event { [super touchesCancelled:touches withEvent:event]; self.state = UIGestureRecognizerStateFailed; } - (void)timeoutMethod { self.state = UIGestureRecognizerStateFailed; } @end </code></pre> <p>You can use it like this:</p> <pre><code>PbDoubleTapGestureRecognizer *doubleTapGr = [[PbDoubleTapGestureRecognizer alloc]initWithTarget:self action:@selector(_doubleTapAction)]; doubleTapGr.maximumDoubleTapDuration = 0.4; [yourView addGestureRecognizer:doubleTapGr]; </code></pre> <p>You can combine this with <code>requireGestureRecognizerToFail:</code> to get the behavior that was asked for.</p>
 

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