iOS开发中六种手势识别
iOS开发中手势识别有六种:
轻击手势(TapGestureRecognizer),
轻扫手势 (SwipeGestureRecognizer),
长按手势(LongPressGestureRecognizer),
拖动手势(PanGestureRecognizer),
捏合手势(PinchGestureRecognizer),
旋转手势(RotationGestureRecognizer),
1,轻击手势(TapGestureRecognizer)
- UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];
- tapGesture.numberOfTapsRequired = 1; //点击次数
- tapGesture.numberOfTouchesRequired = 1; //点击手指数
- [self.view addGestureRecognizer:tapGesture];
- //轻击手势触发方法
- -(void)tapGesture:(UITapGestureRecognizer *)sender
- {
- //your code
- }
2,长按手势(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
3,轻扫手势(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)
- {
- //向右轻扫
- }
- }
4,捏合手势(PinchGestureRecognizer)
- UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchGesture:)];
- [self.view addGestureRecognizer:pinchGesture];
- ////捏合手势触发方法
- -(void) pinchGesture:(id)sender
- {
- UIPinchGestureRecognizer *gesture = sender;
- //手势改变时
- if (gesture.state == UIGestureRecognizerStateChanged)
- {
- //捏合手势中scale属性记录的缩放比例
- _imageView.transform = CGAffineTransformMakeScale(gesture.scale, gesture.scale);
- }
- //结束后恢复
- if(gesture.state==UIGestureRecognizerStateEnded)
- {
- [UIView animateWithDuration:0.5 animations:^{
- _imageView.transform = CGAffineTransformIdentity;//取消一切形变
- }];
- }
- }
5,拖动手势(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
- }
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:1 animations:^{
- _imageView.transform=CGAffineTransformIdentity;//取消形变
- }];
- }
- }
iOS开发中六种手势识别的更多相关文章
- 总结iOS开发中的断点续传那些事儿
前言 断点续传概述 断点续传就是从文件赏赐中断的地方重新开始下载或者上传数据,而不是从头文件开始.当下载大文件的时候,如果没有实现断点续传功能,那么每次出现异常或者用户主动的暂停,都会从头下载,这样很 ...
- iOS开发中静态库之".framework静态库"的制作及使用篇
iOS开发中静态库之".framework静态库"的制作及使用篇 .framework静态库支持OC和swift .a静态库如何制作可参照上一篇: iOS开发中静态库之" ...
- iOS开发中静态库制作 之.a静态库制作及使用篇
iOS开发中静态库之".a静态库"的制作及使用篇 一.库的简介 1.什么是库? 库是程序代码的集合,是共享程序代码的一种方式 2.库的类型? 根据源代码的公开情况,库可以分为2种类 ...
- ios开发中的小技巧
在这里总结一些iOS开发中的小技巧,能大大方便我们的开发,持续更新. UITableView的Group样式下顶部空白处理 //分组列表头部空白处理 UIView *view = [[UIViewal ...
- IOS 开发中 Whose view is not in the window hierarchy 错误的解决办法
在 IOS 开发当中经常碰到 whose view is not in the window hierarchy 的错误,该错误简单的说,是由于 "ViewController" ...
- [转]iOS开发中的火星坐标系及各种坐标系转换算法
iOS开发中的火星坐标系及各种坐标系转换算法 源:https://my.oschina.net/u/2607703/blog/619183 其原理是这样的:保密局开发了一个系统,能将实际的坐标转 ...
- iOS开发中常见问题集锦
在iOS开发中,会出现各种各样的问题.今天,就把这些常见的问题以及各位大牛的解决方案汇总下,方便以后查阅: 常见错误: 1. linker command failed with exit code ...
- iOS开发中获取WiFi相关信息
iOS 开发中难免会遇到很多与网络方面的判断,这里做个汇总,大多可能是与WiFi相关的. 1.Ping域名.Ping某IP 有 时候可能会遇到ping 某个域名或者ip通不通,再做下一步操作.这里的p ...
- iOS开发中设置UITextField的占位文字的颜色,和光标的颜色
在iOS开发中,对于很多初学者而言,很有可能碰到需要修改UITextField的占位文字的颜色,以及当UITextField成为第一响应者后光标的颜色,那么下面小编就介绍一下修改占位文字和光标的颜色. ...
随机推荐
- TIF转JPG
public void TifToJpg(string tifPath, string tifName) { try { //找到后缀为TIF的图像,如果没有,就catch退出 int len = t ...
- Python -- 函数之推导式
5.12 推导式 l = [] for i in range(1,11): l.append(i) print(l) # 用列表推导式 (一行搞定) l = [i for i in range(1,1 ...
- JQuery EasyUI学习记录(一)
1.主页设计(JQuery EasyUI插件) 下载easyUI开发包: 将easyUI资源文件导入页面中: <link rel="stylesheet" type=&quo ...
- HTML5中Web存储
HTML5 中web存储是一个比cookies更好的一个本地存储方式. 那么什么是HTML5存储呢? 使用HTML5可以在本地存储用户浏览的数据,HTML5技术没有出来之前是使用cookies进行本地 ...
- webservice基础
一.webservice概念 webservice用于异构平台之间的交互,我用Java写的程序,可以用php..net.pythod等其它语言的程序来访问我的接口.webservice有很多框架帮我们 ...
- 空类生成对象输出的结果是什么? toString()输出 覆写Object toString()方法输出的结果是什么
空类生成对象输出的结果是什么? 输出的是对象在内存空间地址的哈希值 com.swift.P@1db9742 空类生成对象toString()输出的结果是什么? 输出的是对象在内存空间地址的哈希值的字符 ...
- NSCharacterSet去除字符串中的空格、删除指定\任意字符集
一.去除首尾的空格 /** 1.去除首尾的空格*/ NSString *strMsg=@" 简书作者:CoderZb "; NSString *strResult = [strMs ...
- Unity3d 判断物体是否在可见范围内
unity中自带两个回调函数: void OnBecameVisible()//当物体可见时,回调一次. void OnBecameInvisible()//当物体不可见时,回调一次. 在untiy编 ...
- sqlserver的实例名忘记了
电脑图标右击/管理/服务和应用程序/服务 也可以直接services.msc打开 打开服务,找到sqlserver的服务,这个服务括号中的名称就是实例名了,但是要加上localhost,也就是loca ...
- shell 练习题
1.编写脚本/bin/per.sh,判断当前用户对指定参数文件,是否不可读并且不可写 read -p "Please Input A File: " file if [ ! -e ...