公司項目里用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>。
在看這個代碼里的時候突然對UITapGestureRecognizer的UIGestureRecognizerState感到很迷惑。
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.
從這里可以知道,UITapGestureRecognizer是discrete手勢,不是continuous手勢,因而UITapGestureRecognizer只有3種UIGestureRecognizerState即UIGestureRecognizerStatePossible UIGestureRecognizerStateRecognized 和UIGestureRecognizerStateFailed。而continuous手勢的UIGestureRecognizerState為UIGestureRecognizerStatePossible UIGestureRecognizerStateBegan UIGestureRecognizerStateChanged UIGestureRecognizerStateEnded 和UIGestureRecognizerStateCancelled
到此,所有的疑惑都解決了。