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属性

 @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

IOS UIPanGestureRecognizer手势使用及识别状态UIGestureRecognizerState的更多相关文章

  1. [BS-25] IOS中手势UIGestureRecognizer概述

    IOS中手势UIGestureRecognizer概述 一.概述 iPhone中处理触摸屏的操作,在3.2之前是主要使用的是由UIResponder而来的如下4种方式: - (void)touches ...

  2. iOS学习笔记06-手势识别

    一.UIGestureRecognizer简单介绍 我们已经学习了触摸事件处理,但触摸事件处理起来很麻烦,每个触摸事件处理都需要实现3个touches方法,比较繁琐,实际上我们可以使用更加简单的触摸事 ...

  3. 关于iOS的手势UIGestureRecognizer问题

    typedef NS_ENUM(NSInteger, UIGestureRecognizerState) { UIGestureRecognizerStatePossible, // 尚未识别是何种手 ...

  4. 点击事件touches与ios的手势UIGestureRecognizer

    .h文件 @property (weak,nonatomic) IBOutlet UILabel *messageLabel;@property (weak,nonatomic) IBOutlet U ...

  5. IOS各种手势操作实例

    先看下效果 手势相关的介绍 IOS中手势操作一般是 UIGestureRecognizer 类的几个手势子类去实现,一般我们用到的手势就这么5种: 1.点击  UITapGestureRecogniz ...

  6. IOS中手势UIGestureRecognizer

    通常在对视图进行缩放移动等操作的时候我们可以用UIScrollView,因为它里边自带了这些功能,我们要做的就是告诉UIScrollView的几个相关参数就可以了 但是没有实现旋转的手势即UIRota ...

  7. iOS 九宫格手势密码

    代码地址如下:http://www.demodashi.com/demo/11490.html 一.准备工作 需要准备什么环境 xcode,iOS8+ 本例子实现什么功能 主要实现手势密码设置,验证 ...

  8. iOS开发之记录用户登录状态

    iOS开发之记录用户登录状态 我们知道:CoreData的配置和使用步骤还是挺复杂的.但熟悉CoreData的使用流程后,CoreData还是蛮好用的.今天要说的是如何记录我们用户的登陆状态.例如微信 ...

  9. iOS开发多线程篇—线程的状态

    iOS开发多线程篇—线程的状态 一.简单介绍 线程的创建: self.thread=[[NSThread alloc]initWithTarget:self selector:@selector(te ...

随机推荐

  1. Js 类继承 extends

    html 及 js 代码 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&q ...

  2. 02-12Android学习进度报告十二

    今天学习了ListView的焦点问题,基本了解了ListView的使用内容. 首先可以为抢占了控件的组件设置:android:focusable="false" 只需为抢占了Lis ...

  3. 「CQOI2009」中位数

    「CQOI2009」中位数 传送门 这道题将会用到一点桶的思想. 首先我们可以在排列中先找到 \(b\) 的位置(找不到的话就直接输出 \(0\)). 然后我们从 \(b\) 的位置(设为 \(p\) ...

  4. vue父孙组件传值($attr及$listeners)的使用

    父组件 <template> <div> <!-- 将值传给子组件 子组件可以获取自己想要的值 也可以忽视掉需要传给孙子组件的值 --> <!-- 同时获取通 ...

  5. PAT T1019 Separate the Animals

    暴力搜索加剪枝,二进制保存状态,set去重~ #include<bits/stdc++.h> using namespace std; ; string s[maxn]; struct n ...

  6. SpringBoot报错笔记

    异常一: 1.访问所有方法路径都返回一个page: 截图: 出错原因:不知道 解决方法:新建项目 异常二: 提交表单信息报错 原因:映射文件和和表单的提交方式不统一 解决方法:统一方式即可: 错误三: ...

  7. C. Swap Letters 01字符串最少交换几次相等

    C. Swap Letters time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

  8. 查看mysql进程

    show processlist; show full processlist;

  9. C++ 判断是文件还是文件夹

    转载:https://www.csdn.net/gather_23/NtDaIg1sMDYtYmxvZwO0O0OO0O0O.html Windows平台代码如下: #include <wind ...

  10. vs code 本地调试配置

    { "name": "使用本机 Chrome 调试", "type": "chrome", "request& ...