UIGestureRecognizerState -- 手勢識別器狀態
1.先來看官方文檔
定義UIGestureRecognizer.h
英文:
typedef NS_ENUM(NSInteger, UIGestureRecognizerState) {
UIGestureRecognizerStatePossible, // the recognizer has not yet recognized its gesture, but may be evaluating touch events. this is the default state
UIGestureRecognizerStateBegan, // the recognizer has received touches recognized as the gesture. the action method will be called at the next turn of the run loop
UIGestureRecognizerStateChanged, // the recognizer has received touches recognized as a change to the gesture. the action method will be called at the next turn of the run loop
UIGestureRecognizerStateEnded, // the recognizer has received touches recognized as the end of the gesture. the action method will be called at the next turn of the run loop and the recognizer will be reset to UIGestureRecognizerStatePossible
UIGestureRecognizerStateCancelled, // the recognizer has received touches resulting in the cancellation of the gesture. the action method will be called at the next turn of the run loop. the recognizer will be reset to UIGestureRecognizerStatePossible
UIGestureRecognizerStateFailed, // the recognizer has received a touch sequence that can not be recognized as the gesture. the action method will not be called and the recognizer will be reset to UIGestureRecognizerStatePossible
// Discrete Gestures – gesture recognizers that recognize a discrete event but do not report changes (for example, a tap) do not transition through the Began and Changed states and can not fail or be cancelled
UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded // the recognizer has received touches recognized as the gesture. the action method will be called at the next turn of the run loop and the recognizer will be reset to UIGestureRecognizerStatePossible
};
中文翻譯:
typedef NS_ENUM(NSInteger, UIGestureRecognizerState) {//手勢識別器狀態(由UIGestureRecognizer識別器接收識別), 枚舉類型
UIGestureRecognizerStatePossible, // 識別器還沒有識別出它的手勢(狀態)(Possible),但是可能計算觸摸事件。這是默認狀態
UIGestureRecognizerStateBegan, // 識別器已經接收識別為此手勢(狀態)的觸摸(Began)。在下一輪run循環中,響應方法將會被調用。
UIGestureRecognizerStateChanged, // 識別器已經接收到觸摸,並且識別為手勢改變(Changed)。在下一輪run循環中,響應方法將會被調用。
UIGestureRecognizerStateEnded, // 識別器已經接收到觸摸,並且識別為手勢結束(Ended)。在下一輪run循環中,響應方法將會被調用並且識別器將會被重置到UIGestureRecognizerStatePossible狀態。
UIGestureRecognizerStateCancelled, // 識別器已經接收到觸摸,這種觸摸導致手勢取消(Cancelled)。在下一輪run循環中,響應方法將會被調用。識別器將會被重置到UIGestureRecognizerStatePossible狀態。
UIGestureRecognizerStateFailed, // 識別器已經接收到一個觸摸序列,不能識別為手勢(Failed)。響應方法將不會被調用,並且識別器將會重置到UIGestureRecognizerStatePossible。
// 離散手勢 - 手勢識別器識別一個離散事件,但是不會報告改變(例如,一個輕擊)不會過度到Began和Changed狀態,並且不會失敗(fail)或者被取消(cancell)
UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded // 識別器接收觸摸,並且識別為此手勢。在下一輪run循環中,響應方法將會被調用,並且識別將會重置至UIGestureRecognizerStatePossible。
};
2.名詞釋義
手勢識別中,run循環是什么[1]?
APP啟動后,IOS會自動創建一個主線程 RunLoop,當發生觸摸/鎖屏/搖晃等硬件事件(Event)時,系統封裝這些事件,並發送給 UIWindow等,交由應用程序進行處理,並由系統回調。這個主線程RunLoop就是run循環。
3.手勢
系統自帶的預定義手勢識別器包括(見官方文檔)
UIGestureRecognizer
- UITapGestureRecognizer(Tap 輕擊手勢識別器,輕輕點擊)
- UIPinchGestureRecognizer(Pinch 縮放手勢識別器)
- UIRotationGestureRecognizer(Rotation 旋轉手勢識別器)
- UISwipeGestureRecognizer(Swipe 輕掃手勢識別器,快速滑動)
- UIPanGestureRecognizer(Pan 平移手勢識別器)
- UIScreenEdgePanGestureRecognizer(ScreenEdgePan 屏幕邊緣平移手勢識別器)
- UILongPressGestureRecognizer(LongPress 長按手勢識別器)
4.使用方法
以Pan為例
step1:新建IOS工程,工程名Gesture(任意取名)
step2:在Main.storyboard中添加一個View
如圖1所示

step3: 在ViewController.m中添加屬性, 將View連接至ViewController的mContentView屬性
1 @interface ViewController ()<UIGestureRecognizerDelegate> 2 @property (nonatomic , strong) UIPanGestureRecognizer *panGestureRecognizer;//Pan手勢識別器 3 @property (nonatomic, assign) CGPoint panStartPoint;//記錄觸摸起始點 4 @property (weak, nonatomic) IBOutlet UIView *mContentView;//View連接口 5 @end
step4: 初始化 Pan手勢識別器
1 - (void)viewDidLoad { 2 [super viewDidLoad]; 3 self.panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureRecognize:)];//初始化平移手勢識別器(Pan) 4 self.panGestureRecognizer.delegate = self; 5 [self.mContentView addGestureRecognizer:self.panGestureRecognizer]; 6 }
step5:手勢狀態處理
1 -(void)panGestureRecognize:(UIPanGestureRecognizer *)recognize{ 2 switch (recognize.state) { 3 case UIGestureRecognizerStateBegan: 4 self.panStartPoint = [recognize translationInView:self.mContentView]; 5 NSLog(@"-----Current State: Began-----"); 6 NSLog(@"start point (%f, %f) in View", self.panStartPoint.x, self.panStartPoint.y); 7 break; 8 9 case UIGestureRecognizerStateChanged: 10 NSLog(@"-----Current State: Changed-----"); 11 CGPoint currentPoint = [recognize translationInView:self.mContentView]; 12 NSLog(@"current point (%f, %f) in View", currentPoint.x, currentPoint.y); 13 break; 14 15 case UIGestureRecognizerStateEnded: 16 NSLog(@"-----Current State: Ended-----"); 17 CGPoint endPoint = [recognize translationInView:self.mContentView]; 18 NSLog(@"end point (%f, %f) in View", endPoint.x, endPoint.y); 19 break; 20 21 case UIGestureRecognizerStateCancelled: 22 NSLog(@"-----Current State: Cancelled-----"); 23 NSLog(@"Touch was cancelled"); 24 break; 25 26 case UIGestureRecognizerStateFailed: 27 NSLog(@"-----Current State: Failed-----"); 28 NSLog(@"Failed events"); 29 break; 30 default: 31 break; 32 } 33 }
5. 測試結果
1 2015-07-24 00:28:51.998 Gesture[4039:189604] -----Current State: Began----- 2 2015-07-24 00:28:51.999 Gesture[4039:189604] start point (0.000000, 0.000000) in View 3 2015-07-24 00:28:52.015 Gesture[4039:189604] -----Current State: Changed----- 4 2015-07-24 00:28:52.015 Gesture[4039:189604] current point (1.500000, 0.000000) in View 5 2015-07-24 00:28:52.015 Gesture[4039:189604] -----Current State: Changed----- 6 2015-07-24 00:28:52.016 Gesture[4039:189604] current point (1.500000, 0.000000) in View 7 2015-07-24 00:28:52.032 Gesture[4039:189604] -----Current State: Changed----- 8 2015-07-24 00:28:52.032 Gesture[4039:189604] current point (2.500000, 0.000000) in View 9 2015-07-24 00:28:52.049 Gesture[4039:189604] -----Current State: Changed----- 10 2015-07-24 00:28:52.049 Gesture[4039:189604] current point (3.500000, 0.000000) in View 11 2015-07-24 00:28:52.085 Gesture[4039:189604] -----Current State: Changed----- 12 2015-07-24 00:28:52.086 Gesture[4039:189604] current point (4.500000, 0.000000) in View 13 2015-07-24 00:28:52.111 Gesture[4039:189604] -----Current State: Changed----- 14 2015-07-24 00:28:52.111 Gesture[4039:189604] current point (5.500000, 0.000000) in View 15 2015-07-24 00:28:52.128 Gesture[4039:189604] -----Current State: Changed----- 16 2015-07-24 00:28:52.128 Gesture[4039:189604] current point (6.500000, 0.000000) in View 17 2015-07-24 00:28:52.145 Gesture[4039:189604] -----Current State: Changed----- 18 2015-07-24 00:28:52.145 Gesture[4039:189604] current point (7.500000, 0.000000) in View 19 2015-07-24 00:28:52.163 Gesture[4039:189604] -----Current State: Changed----- 20 2015-07-24 00:28:52.163 Gesture[4039:189604] current point (9.500000, 0.000000) in View 21 2015-07-24 00:28:52.180 Gesture[4039:189604] -----Current State: Changed----- 22 2015-07-24 00:28:52.180 Gesture[4039:189604] current point (10.500000, 0.000000) in View 23 2015-07-24 00:28:52.198 Gesture[4039:189604] -----Current State: Changed----- 24 2015-07-24 00:28:52.198 Gesture[4039:189604] current point (11.500000, 0.000000) in View 25 2015-07-24 00:28:52.215 Gesture[4039:189604] -----Current State: Changed----- 26 2015-07-24 00:28:52.215 Gesture[4039:189604] current point (13.000000, 0.000000) in View 27 2015-07-24 00:28:52.232 Gesture[4039:189604] -----Current State: Changed----- 28 2015-07-24 00:28:52.232 Gesture[4039:189604] current point (15.000000, 0.000000) in View 29 2015-07-24 00:28:52.249 Gesture[4039:189604] -----Current State: Changed----- 30 2015-07-24 00:28:52.249 Gesture[4039:189604] current point (16.000000, -1.500000) in View 31 2015-07-24 00:28:52.267 Gesture[4039:189604] -----Current State: Changed----- 32 2015-07-24 00:28:52.267 Gesture[4039:189604] current point (17.000000, -1.500000) in View 33 2015-07-24 00:28:52.284 Gesture[4039:189604] -----Current State: Changed----- 34 2015-07-24 00:28:52.284 Gesture[4039:189604] current point (18.000000, -1.500000) in View 35 2015-07-24 00:28:52.301 Gesture[4039:189604] -----Current State: Changed----- 36 2015-07-24 00:28:52.302 Gesture[4039:189604] current point (20.500000, -1.500000) in View 37 2015-07-24 00:28:52.318 Gesture[4039:189604] -----Current State: Changed----- 38 2015-07-24 00:28:52.318 Gesture[4039:189604] current point (21.000000, -1.500000) in View 39 2015-07-24 00:28:52.335 Gesture[4039:189604] -----Current State: Changed----- 40 2015-07-24 00:28:52.335 Gesture[4039:189604] current point (22.000000, -1.500000) in View 41 2015-07-24 00:28:52.353 Gesture[4039:189604] -----Current State: Changed----- 42 2015-07-24 00:28:52.353 Gesture[4039:189604] current point (23.000000, -1.500000) in View 43 2015-07-24 00:28:52.371 Gesture[4039:189604] -----Current State: Changed----- 44 2015-07-24 00:28:52.371 Gesture[4039:189604] current point (24.000000, -1.500000) in View 45 2015-07-24 00:28:52.678 Gesture[4039:189604] -----Current State: Ended----- 46 2015-07-24 00:28:52.678 Gesture[4039:189604] end point (24.000000, -1.500000) in View
reference:
[1] http://blog.sina.com.cn/s/blog_14fdb5d190102vrnl.html
