區(qū)分單擊和雙擊手勢

公司項目里用URBMediaFocusViewController

animates thumbnail previews of your media to their full size versions with physics similar to Tweetbot 3.

結果當單擊圖片后發(fā)現(xiàn)圖片的彈出速度比較慢。后來發(fā)現(xiàn)是因為同時在圖片上添加了單擊和雙擊倆種手勢。為了區(qū)分單擊和雙擊使用了[singleTapGestureRecognizer requireGestureRecognizerToFail: doubleTapGestureRecognizer];方法,但是這個區(qū)分單擊和雙擊的時間有點長,因而導致了圖片彈出速度有點慢。根據(jù)How to recognize oneTap/doubleTap at moment? 這個答案改寫出下面的代碼。

#import "HBJFShortTapGestureRecognizer.h"

#define DELAY_SECONDS 0.28

@implementation HBJFShortTapGestureRecognizer

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event];

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(DELAY_SECONDS * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        if (self.state != UIGestureRecognizerStateRecognized) {
            self.state = UIGestureRecognizerStateFailed;
        }

    });

}

@end

注意:在頭文件里要添加#import <UIKit/UIGestureRecognizerSubclass.h>。

在看這個代碼里的時候突然對UITapGestureRecognizerUIGestureRecognizerState感到很迷惑。

typedef enum {
   UIGestureRecognizerStatePossible,
   
   UIGestureRecognizerStateBegan,
   UIGestureRecognizerStateChanged,
   UIGestureRecognizerStateEnded,
   UIGestureRecognizerStateCancelled,
   
   UIGestureRecognizerStateFailed,
   
   UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded
} UIGestureRecognizerState;

上面是所有的UIGestureRecognizerState,而且里面UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded很是讓我不解。在detecting finger up/down UITapGestureRecognizer上面查到

UITapGestureRecognizer is a discrete gesture recognizer, and therefore never transitions to the began or changed states. From the UIGestureRecognizer Class Reference:

Discrete gestures transition from Possible to either Recognized (UIGestureRecognizerStateRecognized) or Failed (UIGestureRecognizerStateFailed), depending on whether they successfully interpret the gesture or not. If the gesture recognizer transitions to Recognized, it sends its action message to its target.

從這里可以知道,UITapGestureRecognizerdiscrete手勢,不是continuous手勢,因而UITapGestureRecognizer只有3種UIGestureRecognizerStateUIGestureRecognizerStatePossible UIGestureRecognizerStateRecognizedUIGestureRecognizerStateFailed。而continuous手勢的UIGestureRecognizerStateUIGestureRecognizerStatePossible UIGestureRecognizerStateBegan UIGestureRecognizerStateChanged UIGestureRecognizerStateEndedUIGestureRecognizerStateCancelled

到此,所有的疑惑都解決了。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容