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 ...
随机推荐
- Cisco Spectrum Expert(Wave2 AP)
在一些版本中,我们可能会发现,AP16,26或AP17,27,37等支持Spectrum Expert Connect (即SE-Connect),该模式可以让AP将频谱分析所述数据发送到对应的分析仪 ...
- 1082 Read Number in Chinese (25分)
// 1082.cpp : 定义控制台应用程序的入口点. // #include <iostream> #include <string> #include <vecto ...
- easy flash &easy log
EASY FLASH: ENV 快速保存产品参数(k-v型no-sql数据库存储),支持 写平衡(磨损平衡) 及 掉电保护 功能 EasyFlash不仅能够实现对产品的 设定参数 或 运行日志 等信息 ...
- Django 中的时区
Django 中的时区 在现实环境中,存在有多个时区.用户之间很有可能存在于不同的时区,并且许多国家都拥有自己的一套夏令时系统.所以如果网站面向的是多个时区用户,只以当前时间为标准开发,便会在时间计算 ...
- 树莓派4B踩坑指南 - (10)安装坚果云(更新:暂不支持)
191209更新: 根据坚果云用户支持(helpdesk@nutstore.net)的官方回复,客户端不支持arm,所以本篇后续内容可以不用看了.. 原文如下: "您好,客户端似乎不支持ar ...
- 了解jQuery
前言-- 通过这篇文章[https://www.cnblogs.com/cchHers/p/9880439.html]了解到JavaScript是编写控制器这种角色语言.文章中也提到了web开始是一门 ...
- IDEA快捷键/本文仅供自己参考使用如有侵权立删
好好来学习学习IDEA这款神器,让你的效率飞起!视频来自慕课网 本文转载 更多技巧 代码定位 跳转: 1.IDEA的左侧侧边栏有1:Project.7:Structure和2:Favorities a ...
- 爬虫模拟cookie自动登录(人人网自动登录)
什么是cookie? 在网站中,HTTP请求时无状态的,也就是说即使第一次和服务器连接后并且登录成功后,第二次请求服务器依然不能知道当前请求是谁,cookie的出现就是为了解决这个问题,第一次登陆后服 ...
- day 11 笔记
# 装饰器形成的过程 : 最简单的装饰器 有返回值的 有一个参数 万能参数 # 装饰器的作用 # 原则 :开放封闭原则 # 语法糖 :@ # 装饰器的固定模式 #不懂技术 import time # ...
- 杭电 2028 ( Lowest Common Multiple Plus )
链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2028 题目要求:就是求最大公倍数,我百度了一下,最好实现的算法就是: 公式法 由于 ...