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循环是什么?

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所示

IOS UIPanGestureRecognizer手势使用及识别状态UIGestureRecognizerState-LMLPHP

step3: 在ViewController.m中添加属性, 将View连接至ViewController的mContentView属性

 @interface ViewController ()<UIGestureRecognizerDelegate>
@property (nonatomic , strong) UIPanGestureRecognizer *panGestureRecognizer;//Pan手势识别器
@property (nonatomic, assign) CGPoint panStartPoint;//记录触摸起始点
@property (weak, nonatomic) IBOutlet UIView *mContentView;//View连接口
@end

step4: 初始化 Pan手势识别器

 - (void)viewDidLoad {
[super viewDidLoad];
self.panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureRecognize:)];//初始化平移手势识别器(Pan)
self.panGestureRecognizer.delegate = self;
[self.mContentView addGestureRecognizer:self.panGestureRecognizer];
}

step5:手势状态处理

 -(void)panGestureRecognize:(UIPanGestureRecognizer *)recognize{
switch (recognize.state) {
case UIGestureRecognizerStateBegan:
self.panStartPoint = [recognize translationInView:self.mContentView];
NSLog(@"-----Current State: Began-----");
NSLog(@"start point (%f, %f) in View", self.panStartPoint.x, self.panStartPoint.y);
break; case UIGestureRecognizerStateChanged:
NSLog(@"-----Current State: Changed-----");
CGPoint currentPoint = [recognize translationInView:self.mContentView];
NSLog(@"current point (%f, %f) in View", currentPoint.x, currentPoint.y);
break; case UIGestureRecognizerStateEnded:
NSLog(@"-----Current State: Ended-----");
CGPoint endPoint = [recognize translationInView:self.mContentView];
NSLog(@"end point (%f, %f) in View", endPoint.x, endPoint.y);
break; case UIGestureRecognizerStateCancelled:
NSLog(@"-----Current State: Cancelled-----");
NSLog(@"Touch was cancelled");
break; case UIGestureRecognizerStateFailed:
NSLog(@"-----Current State: Failed-----");
NSLog(@"Failed events");
       break;
default:
break;
}
}

5. 测试结果

 -- ::51.998 Gesture[:] -----Current State: Began-----
-- ::51.999 Gesture[:] start point (0.000000, 0.000000) in View
-- ::52.015 Gesture[:] -----Current State: Changed-----
-- ::52.015 Gesture[:] current point (1.500000, 0.000000) in View
-- ::52.015 Gesture[:] -----Current State: Changed-----
-- ::52.016 Gesture[:] current point (1.500000, 0.000000) in View
-- ::52.032 Gesture[:] -----Current State: Changed-----
-- ::52.032 Gesture[:] current point (2.500000, 0.000000) in View
-- ::52.049 Gesture[:] -----Current State: Changed-----
-- ::52.049 Gesture[:] current point (3.500000, 0.000000) in View
-- ::52.085 Gesture[:] -----Current State: Changed-----
-- ::52.086 Gesture[:] current point (4.500000, 0.000000) in View
-- ::52.111 Gesture[:] -----Current State: Changed-----
-- ::52.111 Gesture[:] current point (5.500000, 0.000000) in View
-- ::52.128 Gesture[:] -----Current State: Changed-----
-- ::52.128 Gesture[:] current point (6.500000, 0.000000) in View
-- ::52.145 Gesture[:] -----Current State: Changed-----
-- ::52.145 Gesture[:] current point (7.500000, 0.000000) in View
-- ::52.163 Gesture[:] -----Current State: Changed-----
-- ::52.163 Gesture[:] current point (9.500000, 0.000000) in View
-- ::52.180 Gesture[:] -----Current State: Changed-----
-- ::52.180 Gesture[:] current point (10.500000, 0.000000) in View
-- ::52.198 Gesture[:] -----Current State: Changed-----
-- ::52.198 Gesture[:] current point (11.500000, 0.000000) in View
-- ::52.215 Gesture[:] -----Current State: Changed-----
-- ::52.215 Gesture[:] current point (13.000000, 0.000000) in View
-- ::52.232 Gesture[:] -----Current State: Changed-----
-- ::52.232 Gesture[:] current point (15.000000, 0.000000) in View
-- ::52.249 Gesture[:] -----Current State: Changed-----
-- ::52.249 Gesture[:] current point (16.000000, -1.500000) in View
-- ::52.267 Gesture[:] -----Current State: Changed-----
-- ::52.267 Gesture[:] current point (17.000000, -1.500000) in View
-- ::52.284 Gesture[:] -----Current State: Changed-----
-- ::52.284 Gesture[:] current point (18.000000, -1.500000) in View
-- ::52.301 Gesture[:] -----Current State: Changed-----
-- ::52.302 Gesture[:] current point (20.500000, -1.500000) in View
-- ::52.318 Gesture[:] -----Current State: Changed-----
-- ::52.318 Gesture[:] current point (21.000000, -1.500000) in View
-- ::52.335 Gesture[:] -----Current State: Changed-----
-- ::52.335 Gesture[:] current point (22.000000, -1.500000) in View
-- ::52.353 Gesture[:] -----Current State: Changed-----
-- ::52.353 Gesture[:] current point (23.000000, -1.500000) in View
-- ::52.371 Gesture[:] -----Current State: Changed-----
-- ::52.371 Gesture[:] current point (24.000000, -1.500000) in View
-- ::52.678 Gesture[:] -----Current State: Ended-----
-- ::52.678 Gesture[:] end point (24.000000, -1.500000) in View

reference:

[1] http://blog.sina.com.cn/s/blog_14fdb5d190102vrnl.html
05-20 17:48