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 ...
随机推荐
- 使用python爬取天气预报,[python入门案例]
# 天气网余姚地区爬虫案例 import requests from lxml import etree class WeatherSpider: def __init__(self): self.u ...
- MYSQL---外键 primary key 作用
https://www.cnblogs.com/x739400043/p/4732158.html 外键和级联 关于外键写的不错的网址:http://blog.csdn.net/lidaasky/ ...
- HDU1875 畅通工程再续
相信大家都听说一个“百岛湖”的地方吧,百岛湖的居民生活在不同的小岛中,当他们想去其他的小岛时都要通过划小船来实现.现在政府决定大力发展百岛湖,发展首先要解决的问题当然是交通问题,政府决定实现百岛湖的全 ...
- Codeforces Round #618 (Div. 2)A. Non-zero
Guy-Manuel and Thomas have an array aa of nn integers [a1,a2,…,an ]. In one step they can add 11 to ...
- 转专业后对于C语言补修的一些体会(2)
第三章,有以下几个比较重要的点: 1. 强制类型转换. 强制类型转换是C语言中一个十分重要的工具,在C语言的使用中,有很多需要用到强制类型转换的地方,比如在除法中,如果想要得到正确的浮点结果,一般要确 ...
- kafka 分区
1. 默认存储 /tmp/kafka-logs test-0 0:分区数
- VUE父子组件相互传值
passer.vue中代码 首先在文件中引入组件 import canvasDraw from '@/components/CanvasDraw/canvasDraw' 局部注册组件:componen ...
- 一、json与jsonp的使用
1.json与jsonp的引入 在ajax中 JSON用来解决数据交换问题,而JSONP来实现跨域. 备注:跨域也可以通过服务器端代理来解决; 理解:JSON是一种数据交换格式,而J ...
- 02.当构造参数过多时使用builder模式
前言 <Effective Java>中文第三版,是一本关于Java基础的书,这本书不止一次有人推荐我看.其中包括我很喜欢的博客园博主五月的仓颉,他曾在自己的博文<给Java程序猿们 ...
- masonry使用添加紧凑、抗压属性
// 内容紧凑 - 优先完全显示内容,且不多占像素. [self.leftLabel setContentHuggingPriority:UILayoutPriorityDefaultHigh for ...