IOS開發之手勢——UIGestureRecognizer 共存
阿新 • • 發佈:2019-01-27
- UITapGestureRecognizer
- UIPinchGestureRecognizer
- UIRotationGestureRecognizer
- UISwipeGestureRecognizer
- UIPanGestureRecognizer
- UILongPressGestureRecognizer
從命名上不難了解這些類別所對應代表的手勢,分別是 Tap(點一下)、Pinch(二指往內或往外撥動)、Rotation(旋轉)、Swipe(滑動,快速移動)、Pan (拖移,慢速移動)以及 LongPress(長按)。這些手勢別在使用上也很簡單,只要在使用前定義並新增到對應的檢視上即可。
// 定義一個 recognizer, 並加到需要偵測該手勢的 UIView 元件上
那麼這個問題有解嗎?答案是肯定的,UIGestureRecognizer
- (void)viewDidLoad { // 單擊的 Recognizer UITapGestureRecognizer* singleRecognizer; singleRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:selfaction:@selector(handleSingleTapFrom)]; singleTapRecognizer.numberOfTapsRequired = 1; // 單擊 [self.view addGestureRecognizer:singleRecognizer]; // 雙擊的 Recognizer UITapGestureRecognizer* double; doubleRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:selfaction:@selector(handleDoubleTapFrom)]; doubleTapRecognizer.numberOfTapsRequired = 2; // 雙擊 [self.view addGestureRecognizer:doubleRecognizer]; // 關鍵在這一行,如果雙擊確定偵測失敗才會觸發單擊 [singleRecognizer requireGestureRecognizerToFail:doubleRecognizer]; [singleRecognizer release]; [doubleRecognizer release];}