手势识别功能(Gesture Recognizer)
1. 手势识别概念
a. iOS 3.2之后,苹果推出了手势识别功能(GestureRecognizer),在触摸事件处理方面,大大简化了开发者的开发难度。
b. 为了完成手势识别,必须借助于手势识别器----UIGestureRecognizer,利用UIGestureRecognizer,能轻松识别用户在某个view上面做的一些常见手势。
2. 手势识别功能
UIGestureRecognizer是一个抽象类,定义了所有手势的基本行为,使用它的子类才能处理具体的手势:
UITapGestureRecognizer(敲击)
UIPinchGestureRecognizer(捏合,用于缩放)
UIPanGestureRecognizer(拖拽)
UISwipeGestureRecognizer(轻扫)
UIRotationGestureRecognizer(旋转)
UILongPressGestureRecognizer(长按)
3. 手势识别用法
a. 每一个手势识别器的用法都这三步:
1> 创建一个手势的对象;
2> 把手势的对象添加到需要手势的view当中;
3> 实现手势的方法;
b. 用法的具体介绍
#import "ViewController.h"
@interfaceViewController ()
@property (weak,nonatomic) IBOutletUIImageView* imageView;
@end
@implementationViewController
- (void)viewDidLoad
{
[superviewDidLoad];
//敲击手势
// 创建敲击手势的对象
UITapGestureRecognizer* tap = [[UITapGestureRecognizeralloc] initWithTarget:selfaction:@selector(tap:)];
// 几根手指
tap.numberOfTouchesRequired = 2;
// 点几次
tap.numberOfTapsRequired = 2;
// 对imageView添加手势
[self.imageViewaddGestureRecognizer:tap];
//轻扫手势
// 创建轻扫手势的对象
UISwipeGestureRecognizer* swipe = [[UISwipeGestureRecognizeralloc]initWithTarget:selfaction:@selector(swipe:)];
UISwipeGestureRecognizer* swipe1 = [[UISwipeGestureRecognizeralloc]initWithTarget:selfaction:@selector(swipe:)];
// 设置手势方向
swipe.direction = UISwipeGestureRecognizerDirectionLeft;
swipe.direction = UISwipeGestureRecognizerDirectionRight;
// 对imageView添加手势
[self.imageViewaddGestureRecognizer:swipe];
[self.imageViewaddGestureRecognizer:swipe1];
//长按手势
UILongPressGestureRecognizer* longPress = [[UILongPressGestureRecognizeralloc]initWithTarget:selfaction:@selector(longPress:)];
// 长按多长时间执行方法
longPress.minimumPressDuration = 2;
// 误差
longPress.allowableMovement = 10;
// 对imageView添加手势
[self.imageViewaddGestureRecognizer:longPress];
//旋转
UIRotationGestureRecognizer* rotation = [[UIRotationGestureRecognizeralloc]initWithTarget:selfaction:@selector(rotation:)];
[self.imageViewaddGestureRecognizer:rotation];
//捏合,用于缩放
UIPinchGestureRecognizer* pinch = [[UIPinchGestureRecognizeralloc]initWithTarget:selfaction:@selector(pinch:)];
[self.imageViewaddGestureRecognizer:pinch];
//拖拽
UIPanGestureRecognizer* pan = [[UIPanGestureRecognizeralloc] initWithTarget:selfaction:@selector(pan:)];
[self.imageViewaddGestureRecognizer:pan];
}
// 敲击
- (void)tap:(UITapGestureRecognizer*)sender
{
NSLog(@"tap");
}
// 轻扫
- (void)swipe:(UISwipeGestureRecognizer*)sender
{
if (sender.direction == UISwipeGestureRecognizerDirectionLeft){
NSLog(@"left");
}
else {
NSLog(@"right");
}
}
// 长按
- (void)longPress:(UILongPressGestureRecognizer*)sender
{
// 想让长按开始的时候执行某个代码,需要判断手势的状态
if (sender.state == UIGestureRecognizerStateBegan){
NSLog(@"longPress");
}
}
UIGestureRecognizerState手势状态是一个枚举
typedef NS_ENUM(NSInteger, UIGestureRecognizerState) {
// 没有触摸事件发生,所有手势识别的默认状态
UIGestureRecognizerStatePossible,
// 一个手势已经开始但尚未改变或者完成时
UIGestureRecognizerStateBegan,
// 手势状态改变
UIGestureRecognizerStateChanged,
// 手势完成
UIGestureRecognizerStateEnded,
// 手势取消,恢复至Possible状态
UIGestureRecognizerStateCancelled,
// 手势失败,恢复至Possible状态
UIGestureRecognizerStateFailed,
// 识别到手势识别
UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded
};
// 旋转
- (void)rotation:(UIRotationGestureRecognizer*)sender
{
self.imageView.transform= CGAffineTransformRotate(self.imageView.transform, sender.rotation);
sender.rotation = 0;
}
// 捏合
- (void)pinch:(UIPinchGestureRecognizer*)sender
{
self.imageView.transform= CGAffineTransformScale(self.imageView.transform, sender.scale,sender.scale);
sender.scale = 1;
}
// 拖拽
- (void)pan:(UIPanGestureRecognizer*)sender
{
//手指拖动imageView位移时的实时位移量;
CGPoint p = [sender translationInView:self.imageView];
NSLog(@"%@",NSStringFromCGPoint(p));
//将imageView按照上面的实时位移量进行位移;
self.imageView.transform= CGAffineTransformTranslate(self.imageView.transform, p.x,p.y);
//每一次实时位移的时候,都让它的位移量归零;
[sender setTranslation:CGPointZeroinView:self.imageView];
}
手势识别功能(Gesture Recognizer)相关推荐
- python基于opencv的手势识别_怎么在Python3.5 中利用OpenCV实现一个手势识别功能
怎么在Python3.5 中利用OpenCV实现一个手势识别功能 发布时间:2020-12-22 11:56:32 来源:亿速云 阅读:67 作者:Leah 怎么在Python3.5 中利用OpenC ...
- 体感游戏 | 手势识别玩飞机大战游戏(二) Python+OpenCV实现简易手势识别功能
后面将分四篇文章来介绍实现手势识别控制飞机大战游戏的功能,它们分别是: 使用Pygame实现简易飞机大战小游戏 使用Python+OpenCV实现简单手势识别 使用OpenCV实现手势识别玩飞机大战游 ...
- 手语也能机器翻译了!机器学习手势识别功能了解一下
华为机器学习(ML Kit)提供手部关键点识别服务,可用于手语识别.手部关键点识别服务能识别手部21个关键点,通过每个手指的方向和手语规则作比较去找手语字母表. 应用场景 手语通常被听力和口语有障碍的 ...
- 100个iOS开发/设计面试题汇总,你将如何作答?
原文: http://www.csdn.net/article/2015-01-19/2823604-ios-interview-questions 常见问题 你昨天/这周学习了什么? 你为什么热衷于 ...
- 100道ios面试题目的总结体会
常见问题 - 你昨天/这周学习了什么? 昨天到目前为止学习了图片,以及视频上传的压缩处理,还有就是instruments上面的一些用法,还有一个自动化测试工具,进行软件的测试处理 常见问题 - 你为什 ...
- ios开发学习-手势交互(Gesture)效果源码分享
qianqianlianmeng ios开发学习-手势交互(Gesture)效果源码分享 All Around Pull View 介绍:实现视图四个方向(上下左右)都能够拖动更新(pull to r ...
- Gesture Recognizers与触摸事件分发
一.Gesture Recognizers Gesture Recognizers是在iOS3.2引入的,可以用来识别手势.简化定制视图事件处理的对象.Gesture Recognizers的基类为U ...
- 周末项目:使用scikit-learn进行手语和静态手势识别
by Sreehari 通过Sreehari 周末项目:使用scikit-learn进行手语和静态手势识别 (Weekend project: sign language and static-ges ...
- [洪流学堂]Hololens开发高级篇2:手势(Gesture)
本教程基于Unity2017.2及Visual Studio 2017 本教程编写时间:2017年12月7日 本文内容提要 当跟踪到用户的手时提供反馈 使用导航手势旋转hologram 当用户的手要离 ...
最新文章
- dz mysql日志清理_Discuz教程:如何准确的清理数据库
- 图解VC++版PE文件解析器源码分析
- win8桌面壁纸路径
- Android 面试 - compileSdkVersion、minSdkVersion、targetSdkVersion、buildToolsVersion
- select html value属性,HtmlSelect.DataValueField 属性 (System.Web.UI.HtmlControls) | Microsoft Docs...
- sklearn自学指南(part1)--Machine Learning in Python
- Spring Boot + Oracle 处理Blob/Clob类型字段实例
- Android项目同步,如何通过gradle任务同步Android项目?
- VS2013开发Windows服务项目
- Python训练文本情感分析模型
- paypal无法提现?最新解决办法(实战教程)!
- 界面原型创建工具Axure使用教程
- 神奇的平面几何定理--康威圆定理
- 好佳居软装十大品牌 各种风格的窗帘这样搭配,瞬间精致美观
- 【贪心算法】-背包问题
- 嵌入式linux学习路线参考(LINUX学习者必看经典)
- BLUES吉他学习笔记004 bluesrv[8]
- 对传统优化算法的一些总结(上)
- android输入法框架分析,Android输入法架构.ppt
- [Linux] 什么是 段错误(吐核)?
热门文章
- 【CodeForces Round #550】A-F | 模拟 | 贪心 | 高精 | BFS | 二分图 | E
- IIC设备驱动程序(六)————SMBus协议
- 李想批国内电动车虚假里程宣传:特斯拉都打到家门口了别自嗨了
- 什么是异常以及什么是异常的处理?
- 反应迟钝的 Resin
- 零基础:21天搞定Python分布爬虫视频教程直接下载
- kubernetes云原生纪元:StatefulSet 状态守护者
- 语义分割matlab实现fcn_语义分割之FCN
- Win10只有飞行模式,没有无线模式(无线网卡异常)的解决方法
- C盘temp目录cab_xxxx_x文件