How to recognize oneTap/doubleTap at moment?

前端 未结 7 788
独厮守ぢ
独厮守ぢ 2020-12-05 05:51

I Know filtering oneTap/doubleTap using a Apple API. code are follows.

UITapGestureRecognizer *doubleTapGestureRecognizer = [[UITapGestureRecognizer alloc]
          


        
7条回答
  •  情歌与酒
    2020-12-05 06:40

    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.

    PbDoubleTapGestureRecognizer.h :

    @interface PbDoubleTapGestureRecognizer : UIGestureRecognizer
    
    @property (nonatomic) NSTimeInterval maximumDoubleTapDuration;
    
    @end
    

    PbDoubleTapGestureRecognizer.m :

    #import "PbDoubleTapGestureRecognizer.h"
    #import 
    
    @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 > 2) {
            self.state = UIGestureRecognizerStateFailed;
        } else if (self.tapCount == 2 && event.timestamp < 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
    

    You can use it like this:

    PbDoubleTapGestureRecognizer *doubleTapGr = [[PbDoubleTapGestureRecognizer alloc]initWithTarget:self action:@selector(_doubleTapAction)];
    doubleTapGr.maximumDoubleTapDuration = 0.4;
    [yourView addGestureRecognizer:doubleTapGr];
    

    You can combine this with requireGestureRecognizerToFail: to get the behavior that was asked for.

提交回复
热议问题