IOS UIPanGestureRecognizer手势使用及识别状态UIGestureRecognizerState
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的更多相关文章
- [BS-25] IOS中手势UIGestureRecognizer概述
IOS中手势UIGestureRecognizer概述 一.概述 iPhone中处理触摸屏的操作,在3.2之前是主要使用的是由UIResponder而来的如下4种方式: - (void)touches ...
- iOS学习笔记06-手势识别
一.UIGestureRecognizer简单介绍 我们已经学习了触摸事件处理,但触摸事件处理起来很麻烦,每个触摸事件处理都需要实现3个touches方法,比较繁琐,实际上我们可以使用更加简单的触摸事 ...
- 关于iOS的手势UIGestureRecognizer问题
typedef NS_ENUM(NSInteger, UIGestureRecognizerState) { UIGestureRecognizerStatePossible, // 尚未识别是何种手 ...
- 点击事件touches与ios的手势UIGestureRecognizer
.h文件 @property (weak,nonatomic) IBOutlet UILabel *messageLabel;@property (weak,nonatomic) IBOutlet U ...
- IOS各种手势操作实例
先看下效果 手势相关的介绍 IOS中手势操作一般是 UIGestureRecognizer 类的几个手势子类去实现,一般我们用到的手势就这么5种: 1.点击 UITapGestureRecogniz ...
- IOS中手势UIGestureRecognizer
通常在对视图进行缩放移动等操作的时候我们可以用UIScrollView,因为它里边自带了这些功能,我们要做的就是告诉UIScrollView的几个相关参数就可以了 但是没有实现旋转的手势即UIRota ...
- iOS 九宫格手势密码
代码地址如下:http://www.demodashi.com/demo/11490.html 一.准备工作 需要准备什么环境 xcode,iOS8+ 本例子实现什么功能 主要实现手势密码设置,验证 ...
- iOS开发之记录用户登录状态
iOS开发之记录用户登录状态 我们知道:CoreData的配置和使用步骤还是挺复杂的.但熟悉CoreData的使用流程后,CoreData还是蛮好用的.今天要说的是如何记录我们用户的登陆状态.例如微信 ...
- iOS开发多线程篇—线程的状态
iOS开发多线程篇—线程的状态 一.简单介绍 线程的创建: self.thread=[[NSThread alloc]initWithTarget:self selector:@selector(te ...
随机推荐
- P1893山峰瞭望
传送门 看完这个题,大家都懂意思吧,然后代码呢,emmmmm #include <bits/stdc++.h> using namespace std; const int maxn = ...
- 吴裕雄--天生自然Numpy库学习笔记:NumPy 数学函数
NumPy 包含大量的各种数学运算的函数,包括三角函数,算术运算的函数,复数处理函数等. NumPy 提供了标准的三角函数:sin().cos().tan(). import numpy as np ...
- debezium、kafka connector 解析 mysql binlog 到 kafak
目的: 需要搭建一个可以自动监听MySQL数据库的变化,将变化的数据捕获处理,此处只讲解如何自动捕获mysql 中数据的变化 使用的技术 debezium :https://debezium.io/d ...
- 【SSM】AppFileUtils
11 package com.kikyo.sys.utils; import java.io.File; import java.io.IOException; import java.io.Inpu ...
- Legal High
不让任何人承担责任,不想看的东西就回避, 但是,如果想夺回值得夸耀的生存方式,就必须看那些不愿意看的现实,必须带着身负重伤的觉悟前进,这才叫做战斗. 有怨言的话去坟墓里说,钱不是全部,钱就是你们向对手 ...
- 吴裕雄--天生自然数据结构与算法:java代码实现常用数据结构——链表Linked List
class Node{ // 定义节点类 private String data ; // 保存节点内容 private Node next ; // 表示保存下一个节点 public Node(St ...
- mybatis 查询标签
语法 参考:http://www.mybatis.org/mybatis-3/zh/dynamic-sql.html <![CDATA[内容]]>: 参考: http://blog.csd ...
- Java基础 -2.3
浮点数类型 所有的数据类型进行自动转型的时候都是由小类型到大类型进行自动转换处理.默认的类型为double,但是也可以定义位数相对较少的float变量 ,此时从赋值的时候就必须采用强制类型转换 pub ...
- C语言入门---第七章 C语言函数
函数就是一段封装好的,可以重复使用的代码,它使得我们的程序更加模块化,不需要编写大量重复的代码.函数可以提前保存起来,并给它起一个独一无二的名字,只要知道它的名字就能使用这段代码.函数还可以接收数据, ...
- .net工作流设计器
源码地址 Github: https://github.com/chengderen/Smartflow-Sharp 简要说明 https://www.smartflow-sharp.com/doc. ...