【iOS】--手势操作
文章目录
- UIGestureRecognizer 的继承关系:
- 使用手势步骤
- UIPanGestureRecognizer(拖动)
- UIPinchGestureRecognizer(拖动)
- UIRotationGestureRecognizer(旋转)
- UITapGestureRecognizer(点按)
- UILongPressGestureRecognizer(长按)
- UISwipeGestureRecognizer(轻扫)
- 手势操作状态
- UIPanGestureRecognizer(拖动)
- UIPinchGestureRecognizer(捏合)
- UIRotationGestureRecognizer(旋转)
- UITapGestureRecognizer(点按)
- UILongPressGestureRecognizer(长按)
- UISwipeGestureRecognizer(轻扫)
UIGestureRecognizer 的继承关系:
使用手势步骤
使用手势很简单,分为三步:
创建手势识别器对象实例。创建时,指定一个回调方法,当手势开始,改变、或结束时,执行回调方法。
设置手势识别器对象实例的相关属性(可选)。
添加到需要识别的 View 中。每个手势只对应一个 View,当屏幕触摸在 View 的边界内时,如果手势和预定的一样,那就会执行回调方法。
注意:一个手势只能对应一个 View,但是一个 View 可以有多个手势。
UIPanGestureRecognizer(拖动)
UILabel* lab = [[UILabel alloc] init];lab.frame = CGRectMake(100, 100, 100, 100);lab.backgroundColor = [UIColor redColor];[self.view addSubview:lab];lab.userInteractionEnabled = YES;UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(press)];[lab addGestureRecognizer:pan];- (void)press {NSLog(@"11111");
}
UIPinchGestureRecognizer(拖动)
- (void)viewDidLoad {[super viewDidLoad];UILabel* lab = [[UILabel alloc] init];lab.frame = CGRectMake(100, 100, 100, 100);lab.backgroundColor = [UIColor redColor];[self.view addSubview:lab];lab.userInteractionEnabled = YES;UIPinchGestureRecognizer *pan = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(press)];[lab addGestureRecognizer:pan];
}
- (void)press {NSLog(@"11111");
}
UIRotationGestureRecognizer(旋转)
@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];UILabel* lab = [[UILabel alloc] init];lab.frame = CGRectMake(100, 100, 100, 100);lab.backgroundColor = [UIColor redColor];[self.view addSubview:lab];lab.userInteractionEnabled = YES;UIRotationGestureRecognizer *pan = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(press)];[lab addGestureRecognizer:pan];
}
- (void)press {NSLog(@"11111");
}
UITapGestureRecognizer(点按)
@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];UILabel* lab = [[UILabel alloc] init];lab.frame = CGRectMake(100, 100, 100, 100);lab.backgroundColor = [UIColor redColor];[self.view addSubview:lab];lab.userInteractionEnabled = YES;UITapGestureRecognizer *pan = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(press)];[lab addGestureRecognizer:pan];
}
- (void)press {NSLog(@"11111");
}
UILongPressGestureRecognizer(长按)
@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];UILabel* lab = [[UILabel alloc] init];lab.frame = CGRectMake(100, 100, 100, 100);lab.backgroundColor = [UIColor redColor];[self.view addSubview:lab];lab.userInteractionEnabled = YES;UILongPressGestureRecognizer *pan = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(press)];[lab addGestureRecognizer:pan];
}
- (void)press {NSLog(@"11111");
}
UISwipeGestureRecognizer(轻扫)
@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];UILabel* lab = [[UILabel alloc] init];lab.frame = CGRectMake(100, 100, 100, 100);lab.backgroundColor = [UIColor redColor];[self.view addSubview:lab];lab.userInteractionEnabled = YES;UISwipeGestureRecognizer *pan = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(press)];[lab addGestureRecognizer:pan];
}
- (void)press {NSLog(@"11111");
}
手势操作状态
typedef NS_ENUM(NSInteger, UIGestureRecognizerState) {UIGestureRecognizerStatePossible, // 尚未识别是何种手势操作(但可能已经触发了触摸事件),默认状态UIGestureRecognizerStateBegan, // 手势已经开始,此时已经被识别,但是这个过程中可能发生变化,手势操作尚未完成UIGestureRecognizerStateChanged, // 手势状态发生转变UIGestureRecognizerStateEnded, // 手势识别操作完成(此时已经松开手指)UIGestureRecognizerStateCancelled, // 手势被取消,恢复到默认状态UIGestureRecognizerStateFailed, // 手势识别失败,恢复到默认状态UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded // 手势识别完成,同UIGestureRecognizerStateEnded};
在六种手势识别中,只有一种手势是离散型手势,就是 UITapGestureRecognizer(点按)。
离散型手势的特点:一旦识别就无法取消,而且只会调用一次手势操作事件(初始化手势时指定的回调方法)。
其他五种手势都是连续型手势。
连续型手势的特点:会多次调用手势操作事件,而且在连续手势识别后可以取消手势。
对于离散型手势 UITapGestureRecgnizer 要么被识别,要么失败,点按(假设点按次数设置为1,并且没有添加长按手势)下去一次不松开则此时什么也不会发生,松开手指立即识别并调用操作事件,并且状态为3(已完成)。
但是连续型手势要复杂一些,就拿旋转手势来说,如果两个手指点下去不做任何操作,此时并不能识别手势(因为我们还没旋转)但是其实已经触发了触摸开始事件,此时处于状态0;如果此时旋转会被识别,也就会调用对应的操作事件,同时状态变成1(手势开始),但是状态1只有一瞬间;紧接着状态变为2(因为我们的旋转需要持续一会),并且重复调用操作事件(如果在事件中打印状态会重复打印2);松开手指,此时状态变为3,并调用1次操作事件。
连续手势发生状态转换是由于触摸事件中的移动事件造成的,苹果官方的分析图也说明了这一点:
【iOS】--手势操作相关推荐
- iOS手势操作简介(一)
iOS中能够响应手势操作的类必须要继承自UIResponder,才能够处理手势响应操作. 默认继承了UIResponder的类有:UIApplication UIViewController UIVi ...
- iOS 手势操作:拖动、捏合、旋转、点按、长按、轻扫、自定义
http://www.cnblogs.com/huangjianwu/p/4675648.html 1.UIGestureRecognizer 介绍 手势识别在 iOS 中非常重要,他极大地提高了移动 ...
- iOS手势操作简介(五)
利用手势操作实现抽屉效果: 第一步:搭建UI (void)addChildView { // left UIView *leftView = [[UIView alloc] initWithFrame ...
- iOS手势操作简介(三)
监听触摸事件的做法 如果想监听一个view上面的触摸事件,之前的做法是 自定义一个view 实现view的touches方法,在方法内部实现具体处理代码 通过touches方法监听view触摸事件,有 ...
- iOS手势操作简介(六)
利用UIGestureRecognizer来对手势进行处理: @interface HMViewController () @property (weak, nonatomic) IBOutlet U ...
- iOS手势操作简介(四)
当事件传递到相应的UIResponder后,会首先调用: hitTest:withEvent: return (UIView *) UIApplication -> UIWindow 什么时候调 ...
- iOS——6种系统手势操作
UIGestureRecognizer(手势识别器) 手势识别在 iOS 中非常重要,他极大地提高了移动设备的使用便捷性. iOS 系统在 3.2 以后,他提供了六种常用的手势(UIGestureRe ...
- iOS开发系列课程(08) --- 事件处理详解和手势操作
iOS中的事件分发 事件的分类 Touch Events(多点触摸事件) touchesBegan:withEvent:方法:一个或多个手指置于视图或窗口上 touchesMoved:withEven ...
- iphonex如何关机_iphonex常用手势操作有哪些 iphonex常用手势操作介绍【详解】
iphonex常用手势操作有什么?相信小伙伴们一定很好奇,下面小编为大家带来了iphonex常用手势操作大全一览,感兴趣的小伙伴赶紧跟着小编一起来看看吧. 如何唤醒Siri 苹果一直追求极简的设计思路 ...
最新文章
- 没有提示_华为手机发出莫名的提示音,打开什么也没有?原来是它们在作怪
- 运行JBoss 5.1.0 GA时出现Error installing to Instantiated:name=AttachmentStore state=Described错误的解决办法...
- bert中的sep_基于向量的深层语义相似文本召回?你需要BERT和Faiss
- 《Excel最强功能-数据透视表》 网课笔记
- 关于B.M.W的最原始的说明
- PAT (Basic Level) 1080 MOOC期终成绩(模拟+stl)
- JavaScript 之 JS重载
- 安装docker和jupyter采坑历程
- 文档还是程序? Smart Document 技术概述
- php 监听redis,swoole如何监听redis数据
- android 实现应用程序后台运行的说明
- 2020 年TI 杯大学生电子设计竞赛-无人机
- easypoi excel模板导出pdf
- python编程最大值_python求最大值最小值方法总结
- Matlab学习1-图像处理灰度
- 互联网深处有趣网站——进阶篇
- git学习(一)初始化
- iPhone又爆Wi-Fi漏洞 中招Wi-Fi就废了
- 高德定位慢 - iOS
- golang mysql 崩溃_使用GoLang与mysql连接失败