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原生实现二维码扫描
最近项目上需要开发扫描二维码进行签到的功能,主要用于开会签到的场景,所以为了避免作弊,我们再开发时只采用直接扫描的方式,并且要屏蔽从相册读取图片,此外还在二维码扫描成功签到时后台会自动上传用户的当前地 ...
随机推荐
- eclipse编辑struts.xml 代码提示
先确定xml文件 window-preferences-查询catalog 点击add 关于这个Location 先找到你下载的struts压缩包 然后找到 解压这个jar包 你会得到一些dtd文件 ...
- DataTable模拟
DataTable dt = new DataTable(); dt.Columns.Add(new DataColumn("timestep", Type.GetType(&qu ...
- [课程设计]Scrum 2.0 多鱼点餐系统开发进度(第二阶段项目构思与任务规划)
[课程设计]Scrum 2.0 多鱼点餐系统开发进度 1.团队名称:重案组 2.团队目标:长期经营,积累客户充分准备,伺机而行 3.团队口号:矢志不渝,追求完美 4.团队选题:餐厅到店点餐系统WEB ...
- vs2013-tfs-疑问之版本控制器路径有双引号解决办法
问题描述: 最近项目:“****”展示交易平台 ,所以版本控制器路径为: 导致生成解决方案提示:路径有问题 解决办法: 1.直接在版本控制器重命名是不支持的,需要安装: Visual Studio ...
- winform开发框架之模块维护
前言:模块维护试图解决的问题, 模块加载只用MEF的方式: MEF(Managed Extensibility Framework)是一个用于创建可扩展的轻型应用程序的库. 应用程序开发人员可利用该库 ...
- Sprint(第七天11.20)
燃尽图
- web缓存
web缓存HTTP协议的一个核心特性,它能最小化网络流量,并且提升用户所感知的整个系统响应速度. 什么能被缓存? *Logo和商标图像 *普通的不变化的图像(例如,导航图标) *CSS样式表 *普通的 ...
- C#相等性比较
本文阐述C#中相等性比较,其中主要集中在下面两个方面 ==和!=运算符,什么时候它们可以用于相等性比较,什么时候它们不适用,如果不使用,那么它们的替代方式是什么? 什么时候,需要自定一个类型的相等性比 ...
- hdu3228Island Explorer
链接 给你两条线及两条线上的点,求最小生成树. 可以挨个枚举一条线上的点,三分出另一条线上离他最近的点进行连边. 注意N.M可能为0 debug了1天半,至今不知道原始二分版本错在哪里.. #incl ...
- mysql简介
1.什么是数据库 ? 数据库(Database)是按照数据结构来组织.存储和管理数据的仓库,它产生于距今六十多年前,随着信息技术和市场的发展,特别是二十世纪九十年代以后,数据管理不再仅仅是存储和管理数 ...