iOS学习之手势
UIGestureRecognizer
为了完成手势识别,必须借助于手势识别器—-UIGestureRecognizer,利用UIGestureRecognizer,能轻松识别用户在某个view上面做的一些常见手势UIGestureRecognizer是一个抽象类,定义了所有手势的基本行为,使用它的子类才能处理具体的手势,要实现<UIGestureRecognizerDelegate>
手势状态:
typedef NS_ENUM(NSInteger, UIGestureRecognizerState) {
UIGestureRecognizerStatePossible, // 尚未识别是何种手势操作(但可能已经触发了触摸事件),默认状态
UIGestureRecognizerStateBegan, // 手势已经开始,此时已经被识别,但是这个过程中可能发生变化,手势操作尚未完成
UIGestureRecognizerStateChanged, // 手势状态发生转变
UIGestureRecognizerStateEnded, // 手势识别操作完成(此时已经松开手指)
UIGestureRecognizerStateCancelled, // 手势被取消,恢复到默认状态
UIGestureRecognizerStateFailed, // 手势识别失败,恢复到默认状态
UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded // 手势识别完成,同UIGestureRecognizerStateEnded };
常用的六种手势:
- 轻击手势(TapGestureRecognizer)
- 轻扫手势 (SwipeGestureRecognizer)
- 长按手势(LongPressGestureRecognizer)
- 拖动手势(PanGestureRecognizer)
- 捏合手势(PinchGestureRecognizer)
- 旋转手势(RotationGestureRecognizer)
1. 轻击手势(TapGestureRecognizer)
) 初始化轻击手势识别器对象 UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init]; ) 设置手势识别器对象的具体属性
// 连续敲击2次
tap.numberOfTapsRequired = ;
// 需要2根手指一起敲击
tap.numberOfTouchesRequired = ;
//添加手势识别器到对应的view上
[self.iconView addGestureRecognizer:tap]; ) 监听手势的触发
[tap addTarget:self action:@selector(tapIconView:)];
) 代理
tap.delegate = self;
//轻击手势触发方法
-(void) tapIconView:(UITapGestureRecognizer *)sender
{ NSLog(@"我用两只手指敲击屏幕成功了!");
}
#pragma mark - 代理方法 /** * 当点击view的时候,会先调用这个方法 */ - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
CGPoint pos = [touch locationInView:touch.view]; if (pos.x <= self.iconView.frame.size.width * 0.5) { return YES; }
return NO; }
2. 轻扫手势 (SwipeGestureRecognizer)
初始化轻扫手势识别器对象
UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)]; //设置轻扫的向右方向 swipeGesture.direction = UISwipeGestureRecognizerDirectionRight; //向右 [self.view addGestureRecognizer:swipeGesture]; UISwipeGestureRecognizer *swipeGestureLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)]; //设置轻扫的方向 swipeGestureLeft.direction = UISwipeGestureRecognizerDirectionLeft; //向左 [self.view addGestureRecognizer:swipeGestureLeft]; //轻扫手势触发方法 -(void)swipeGesture:(id)sender { UISwipeGestureRecognizer *swipe = sender; if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) { //向左轻扫 } if (swipe.direction == UISwipeGestureRecognizerDirectionRight) { //向右轻扫 } }
3. 长按手势(LongPressGestureRecognizer)
初始化轻扫手势识别器对象
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGesture:)];
//设置长按时间
longPressGesture.minimumPressDuration = 0.5;
[self.view addGestureRecognizer:longPressGesture];
//长按手势触发方法
-(void)longPressGesture:(id)sender
{
UILongPressGestureRecognizer *longPress = sender;
if (longPress.state == UIGestureRecognizerStateBegan)
{
//your code
}
}
说明:长按手势的常用状态如下
开始:UIGestureRecognizerStateBegan
改变:UIGestureRecognizerStateChanged
结束:UIGestureRecognizerStateEnded
取消:UIGestureRecognizerStateCancelled
失败:UIGestureRecognizerStateFailed
4. 拖动手势(PanGestureRecognizer)
初始化轻扫手势识别器对象
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGesture:)];
[self.view addGestureRecognizer:panGesture];
//拖动手势触发方法
-(void) panGesture:(id)sender
{
UIPanGestureRecognizer *panGesture = sender;
CGPoint movePoint = [panGesture translationInView:self.view];
//your code
}
5. 捏合手势(PinchGestureRecognizer)
初始化轻扫手势识别器对象 UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGesture:)]; [self.view addGestureRecognizer:panGesture];
//拖动手势触发方法 -(void) panGesture:(id)sender { UIPanGestureRecognizer *panGesture = sender; CGPoint movePoint = [panGesture translationInView:self.view]; //your code
}
6. 旋转手势(RotationGestureRecognizer)
初始化轻扫手势识别器对象
UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationGesture:)];
[self.view addGestureRecognizer:rotationGesture];
//旋转手势触发方法
-(void)rotationGesture:(id)sender
{
UIRotationGestureRecognizer *gesture = sender;
if (gesture.state==UIGestureRecognizerStateChanged)
{
_imageView.transform=CGAffineTransformMakeRotation(gesture.rotation);
}
if(gesture.state==UIGestureRecognizerStateEnded)
{
[UIView animateWithDuration: animations:^{
_imageView.transform=CGAffineTransformIdentity;//取消形变
}];
}
}
iOS学习之手势的更多相关文章
- iOS学习路线图
一.iOS学习路线图 二.iOS学习路线图--视频篇 阶 段 学完后目标 知识点 配套学习资源(笔记+源码+PPT) 密码 基础阶段 学习周期:24天 学习后目标: ...
- ios学习之UISwipeGestureRecognizer手势识别
ios学习之UISwipeGestureRecognizer手势识别 本文部分转自俺是一个瓜娃!!!的博客UISwipeGestureRecognizer ---手指动作,转载过来仅是为了自己查询 ...
- iOS学习笔记-精华整理
iOS学习笔记总结整理 一.内存管理情况 1- autorelease,当用户的代码在持续运行时,自动释放池是不会被销毁的,这段时间内用户可以安全地使用自动释放的对象.当用户的代码运行告一段 落,开始 ...
- iOS开发摇动手势实现详解
1.当设备摇动时,系统会算出加速计的值,并告知是否发生了摇动手势.系统只会运动开始和结束时通知你,并不会在运动发生的整个过程中始终向你报告每一次运动.例如,你快速摇动设备三次,那只会收到一个摇动事件. ...
- iOS学习笔记总结整理
来源:http://mobile.51cto.com/iphone-386851_all.htm 学习IOS开发这对于一个初学者来说,是一件非常挠头的事情.其实学习IOS开发无外乎平时的积累与总结.下 ...
- ios学习-delegate、传值、跳转页面
ios学习-delegate.传值.跳转页面 1.打开xcode,然后选择ios--Application--Empty Application一个空项目. 项目目录: 2.输入项目名称以及选 ...
- iOS学习——UIView的研究
在iOS开发中,我们知道有一个共同的基类——NSObject,但是对于界面视图而言,UIView是非常重要的一个类,UIView是很多视图控件的基类,因此,对于UIView的学习闲的非常有必要.在iO ...
- 2015最新iOS学习线路图
iOS是由苹果公司开发的移动操作系统,以xcode为主要开发工具,具有简单易用的界面.令人惊叹的功能,以及超强的稳定性,已经成为iPhone.iPad 和iPod touch 的强大基础:iOS 内置 ...
- iOS学习——iOS原生实现二维码扫描
最近项目上需要开发扫描二维码进行签到的功能,主要用于开会签到的场景,所以为了避免作弊,我们再开发时只采用直接扫描的方式,并且要屏蔽从相册读取图片,此外还在二维码扫描成功签到时后台会自动上传用户的当前地 ...
随机推荐
- osip状态机分析
转载于:http://blog.csdn.net/lbc2100/article/details/48342889 OSIP的核心是系统状态机,在不同情况下,系统处于不同的状态,在某一状态下当系统发生 ...
- Poj(1251),Prim字符的最小生成树
题目链接:http://poj.org/problem?id=1251 字符用%s好了,方便一点. #include <stdio.h> #include <string.h> ...
- ibatis mybatis sql语句配置 符号不兼容 大于号 小于号<!CDATA[ ]>
ibatis mybatis sql语句配置 符号不兼容 大于号 小于号<!CDATA[ ]> 因为这个是xml格式的,所以不允许出现类似">"这样的字符,但是都 ...
- PCL点云库:Kd树
Kd树按空间划分生成叶子节点,各个叶子节点里存放点数据,其可以按半径搜索或邻区搜索.PCL中的Kd tree的基础数据结构使用了FLANN以便可以快速的进行邻区搜索.FLANN is a librar ...
- Python virtualenv安装库报错SSL: CERTIFICATE_VERIFY_FAILED
Python virtualenv安装库报错SSL: CERTIFICATE_VERIFY_FAILED 问题描述 使用pip按照virtualenv报错,如下: pip install virtua ...
- php 中的常量
1.__FINE__ 返回当前常量所在的行号. 2.__FILE__ 返回文件的完整路径和文件名. 3.__FUNCTION__ 返回函数名称. 4.__CLASS__ 返回类名称. 5.__METH ...
- C++11在时空性能方面的改进
C++11在时空性能方面的改进 这篇我们聊聊C++11在时间和空间上的改进点: 主要包括以下方面: 新增的高效容器:array.forward_list以及unordered containers: ...
- iOS - Mac OS X 终端设置
Mac OS X 终端设置 1)Finder 中显示资源库 方法一: 在 "终端" 中输入下面的命令: 显示: chflags nohidden ~/Library/ 隐藏: ch ...
- tfs 分支
集团-IT部张强 11:15:211.主干时刻处于稳定状态,随时可以发布.设专门人员对主干代码进行管理,普通开发人员只读. 2.为开发任务建立开发分支.常规的可以以小组为单位建立分支,较大的任务可以建 ...
- oracle sql 优化
2. 选择最有效率的表名顺序(只在基于规则的优化器中有效) ORACLE的解析器按照从右到左的顺序处理FROM子句中的表名,因此FROM子句中写在最后的表(基础表 driving table)将被最先 ...