How to recognize oneTap/doubleTap at moment?

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

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

UITapGestureRecognizer *doubleTapGestureRecognizer = [[UITapGestureRecognizer alloc]
          


        
7条回答
  •  旧时难觅i
    2020-12-05 06:39

    The easiest way to do this is to subclass UITapGestureRecognizer and not a general UIGestureRecognizer.

    Like this:

    #import 
    
    #define UISHORT_TAP_MAX_DELAY 0.2
    @interface UIShortTapGestureRecognizer : UITapGestureRecognizer
    
    @end
    

    And simply implement:

    @implementation UIShortTapGestureRecognizer
    
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        [super touchesBegan:touches withEvent:event];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(UISHORT_TAP_MAX_DELAY * NSEC_PER_SEC)), dispatch_get_main_queue(), ^
        {
            // Enough time has passed and the gesture was not recognized -> It has failed.
            if  (self.state != UIGestureRecognizerStateRecognized)
            {
                self.state = UIGestureRecognizerStateFailed;
            }
        });
    }
    @end
    

提交回复
热议问题