iOS------手势操作(nib文件、纯代码)
总共有六种手势识别:轻击手势(TapGestureRecognizer),轻扫手势 (SwipeGestureRecognizer), 长按手势(LongPressGestureRecognizer), 拖动手势(PanGestureRecognizer), 捏合手势(PinchGestureRecognizer),旋转手势(RotationGestureRecognizer);
其实这些手势用touche事件完全可以实现,苹果就是把常用的触摸事件封装成手势,来提供给用户。读者完全可以用TouchesMoved来写拖动手势等
一,用storyboard给控件添加手势识别
1.用storyboard添加手势识别,和添加一个Button的步骤一样,首先我们得找到相应的手势,把手势识别的控件拖到我们要添加手势的控件中,截图如下:

2.给我们拖出的手势添加回调事件,和给Button回调事件没啥区别的,在回调方法中添加要实现的业务逻辑即可,截图如下:

二,纯代码添加手势识别
用storyboard可以大大简化我们的操作,不过纯代码的方式还是要会的,就像要Dreamwear编辑网页一样(当然啦,storyboard的拖拽功能要比Dreamwear的拖拽强大的多),用纯代码敲出来的更为灵活,更加便 于维护。不过用storyboard可以减少我们的工作量,这两个要配合着使用才能大大的提高我们的开发效率。个人感觉用storyboard把框架搭起 来(Controller间的关系),一下小的东西还是用纯代码敲出来更好一些。下面就给出如何给我们的控件用纯代码的方式来添加手势识别。
1.轻击手势(TapGestureRecognizer)的添加
初始化代码TapGestureRecongnizer的代码如下:
|
1
2
3
4
5
6
|
//新建tap手势 UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)]; //设置点击次数和点击手指数 tapGesture.numberOfTapsRequired = 1; //点击次数 tapGesture.numberOfTouchesRequired = 1; //点击手指数 [self.view addGestureRecognizer:tapGesture]; |
在回调方法中添加相应的业务逻辑:
|
1
2
3
4
5
|
//轻击手势触发方法-(void)tapGesture:(id)sender{ //轻击后要做的事情 } |
2.长按手势(LongPressGestureRecognizer)
初始化代码:
|
1
2
3
4
5
|
//添加长摁手势 UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGesture:)]; //设置长按时间 longPressGesture.minimumPressDuration = 0.5; //(2秒) [self.view addGestureRecognizer:longPressGesture]; |
在对应的回调方法中添加相应的方法(当手势开始时执行):
|
1
2
3
4
5
6
7
8
9
10
|
//常摁手势触发方法-(void)longPressGesture:(id)sender{ UILongPressGestureRecognizer *longPress = sender; if (longPress.state == UIGestureRecognizerStateBegan) { UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"长按触发" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles: nil]; [alter show]; }} |
代码说明:手势的常用状态如下
开始:UIGestureRecognizerStateBegan
改变:UIGestureRecognizerStateChanged
结束:UIGestureRecognizerStateEnded
取消:UIGestureRecognizerStateCancelled
失败:UIGestureRecognizerStateFailed
3.轻扫手势(SwipeGestureRecognizer)
在初始化轻扫手势的时候得指定轻扫的方向,上下左右。 如果要要添加多个轻扫方向,就得添加多个轻扫手势,不过回调的是同一个方法。
添加轻扫手势,一个向左一个向右,代码如下:
|
1
2
3
4
5
6
7
8
9
10
11
|
//添加轻扫手势 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]; |
回调方法如下:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
//轻扫手势触发方法-(void)swipeGesture:(id)sender{ UISwipeGestureRecognizer *swipe = sender; if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) { //向左轻扫做的事情 } if (swipe.direction == UISwipeGestureRecognizerDirectionRight) { //向右轻扫做的事情 }} |
4.捏合手势(PinchGestureRecognizer)
捏合手势初始化
|
1
2
3
|
//添加捏合手势UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchGesture:)];[self.view addGestureRecognizer:pinchGesture]; |
捏合手势要触发的方法(放大或者缩小图片):
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
////捏合手势触发方法-(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)
拖动手势的初始化
|
1
2
3
|
//添加拖动手势UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGesture:)];[self.view addGestureRecognizer:panGesture]; |
拖动手势要做的方法(通过translationInView获取移动的点,和TouchesMoved方法类似)
|
1
2
3
4
5
6
7
8
9
|
//拖动手势-(void) panGesture:(id)sender{ UIPanGestureRecognizer *panGesture = sender; CGPoint movePoint = [panGesture translationInView:self.view]; //做你想做的事儿} |
6.旋转手势(RotationGestureRecognizer)
旋转手势的初始化
|
1
2
3
|
//添加旋转手势UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationGesture:)];[self.view addGestureRecognizer:rotationGesture]; |
旋转手势调用的方法:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
//旋转手势-(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------手势操作(nib文件、纯代码)的更多相关文章
- ios开发UI篇—使用纯代码自定义UItableviewcell实现一个简单的微博界面布局
本文转自 :http://www.cnblogs.com/wendingding/p/3761730.html ios开发UI篇—使用纯代码自定义UItableviewcell实现一个简单的微博界面布 ...
- Storyboard、Nib文件和代码来实现UI的利与弊
很清楚,这就是iOS里面两种可视化UI的方法.加上全部用代码来实现UI,总共有三种方法可以来实现. 我们先说一下全用代码来做,这个方法属于比较极端的程序员所推崇的,优点和缺点同样明显. 优点是可以实现 ...
- iOS UICollectionView(转一) XIB+纯代码创建:cell,头脚视图 cell间距
之前用CollectionViewController只是皮毛,一些iOS从入门到精通的书上也是泛泛而谈.这几天好好的搞了搞苹果的开发文档上CollectionViewController的内容,亲身 ...
- iOS 手势操作:拖动、捏合、旋转、点按、长按、轻扫、自定义
1.UIGestureRecognizer 介绍 手势识别在 iOS 中非常重要,他极大地提高了移动设备的使用便捷性. iOS 系统在 3.2 以后,他提供了一些常用的手势(UIGestureReco ...
- iOS手势操作,拖动,轻击,捏合,旋转,长按,自定义(http://www.cnblogs.com/huangjianwu/p/4675648.html)
1.UIGestureRecognizer 介绍 手势识别在 iOS 中非常重要,他极大地提高了移动设备的使用便捷性. iOS 系统在 3.2 以后,他提供了一些常用的手势(UIGestureReco ...
- PHP文件读写操作之文件写入代码
在PHP网站开发中,存储数据通常有两种方式,一种以文本文件方式存储,比如txt文件,一种是以数据库方式存储,比如Mysql,相对于数据库存储,文件存储并没有什么优势,但是文件读写操作在基本的PHP开发 ...
- 猫学习IOS(三)UI纯代码UI——图片浏览器
猫分享.必须精品 看看效果 主要实现相似看新闻的一个界面,不用拖拽,纯代码手工写. 首先分析app能够非常easy知道他这里有两个UILabel一个UIImageView还有两个UIButton 定义 ...
- ios学习笔记 UITableView(纯代码) (一)
参考 “https://www.cnblogs.com/ai-developers/p/4557487.html” UITableViewCell 有一个代码重用 减少资源的浪费 参考 https: ...
- ios手势操作,四个基本事件与六个常用事件
基本事件包括begin,canceled,move,ended四项,如果对象的hidden属性为yes,则无效果,hidden属性必须为no;才能使用: -(void)touchesBegan:(NS ...
随机推荐
- Mysql-数据库及数据表结构和操作
1.数据库系统:数据库系统是用来维护和管理数据库的系统工具,数据库系统拥有自己的用户名和密码 1.1.显示该系统中的数据库:Show databases; 1.2.创建数据库:Create datab ...
- windows10下安装TensorFlow Object Detection API
由于官方的文件都是在乌班图和mac系统下的,就上网搜了以下. 按照下边这个博客安装成功了 https://blog.csdn.net/qq_28019591/article/details/82023 ...
- linux yum安装指定版本mysql
1.下载mysql rpm包 cd /usr/local/src wget https://dev.mysql.com/get/mysql80-community-release-el7-.noarc ...
- Unity中实现全局管理类的几种方式
(搬运自我在SegmentFault的博客) 如何在Unity中实现全局管理类?由于Unity脚本的运行机制和面向组件编程(COP)的思想,实现起来和普通的方式略有差别. 第一种方式是使用静态类.适合 ...
- 字符编码ANSI和ASCII区别、Unicode和UTF-8区别
ANSI码ANSI编码是一种对ASCII码的拓展:ANSI编码用0x00~0x7f (即十进制下的0到127)范围的1 个字节来表示 1 个英文字符,超出一个字节的 0x80~0xFFFF 范围来表示 ...
- halt, reboot, poweroff - 中止系统运行
SYNOPSIS /sbin/halt [-n] [-w] [-d] [-f] [-i] [-p] /sbin/reboot [-n] [-w] [-d] [-f] [-i] /sbin/powero ...
- 【转】在MAC下使用ISO制作Linux的安装USB盘
http://www.linuxidc.com/Linux/2013-04/82973.htm 在Mac环境下,将Linux的ISO镜像生成一个Linux的安装盘,和Linux下差不多,只是Mac下有 ...
- [BZOJ4327]:[JZOI2012]玄武密码(AC自动机)
题目传送门 题目描述: 在美丽的玄武湖畔,鸡鸣寺边,鸡笼山前,有一块富饶而秀美的土地,人们唤作进香河.相传一日,一缕紫气从天而至,只一瞬间便消失在了进香河中.老人们说,这是玄武神灵将天书藏匿在此. ...
- javaweb基础(2)_tomcat服务器配置
一.Tomcat服务器端口的配置 Tomcat的所有配置都放在conf文件夹之中,里面的server.xml文件是配置的核心文件. 如果想修改Tomcat服务器的启动端口,则可以在server.xml ...
- C++ 学习笔记(二) const的加强
const 含义为只读.如果在程序中显式改变const变量那么编译会报错. C语言的const: 在C语言中const 变量是放在内存中,如果使用指针可以间接改变const变量.所以在C语言中cons ...