iOS开发-UITapGestureRecognizer手势
手势在iOS开发中是一个比较常用的功能,不过相对来说大家用的比较少,经常刷网易新闻,上次用了一下捏合手势才发现可以调整字体大小。昨天看到一个介绍摇一摇这个功能的,没看到之前一直都觉得摇一摇是微信的专有的,昨天测试了一下知乎,感觉像发现了一个新大陆,随便截了图,效果如下:
扯的有点远了,很多应用的很多功能其实对于大多数而言是没有用到的,不过作为程序员我们还是应该多研究一下。
基础概念
常见的手势有六种,如下图所示:
UITapGestureRecognizer(点击,轻触摸)、UIPinchGestureRecognizer(二指往內或往外拨动,捏合手势)、UIPanGestureRecognizer(拖移)、UISwipeGestureRecognizer(滑动,快速移动)、UIRotationGestureRecognizer(旋转)和UILongPressGestureRecognizer(长按),由于微信的缘故应该大多数人对长按比较熟悉,Tap点击也是高频用到的手势。
苹果官方给出了Tap和Pinch的手势的效果图,其他的效果可以私下试一试:
Demo实战
由于有六种手势,基本上大同小异,其中一种会实战,其他的应该也没问题,接下来的的介绍都是以UITapGestureRecognizer为基准的,先来个简单的单击手势:
1 2 3 4 | UITapGestureRecognizer *oneTapGestureReognizer = [[UITapGestureRecognizer alloc] initWithTarget: self action: @selector (oneTapGestureRecognizer:)]; oneTapGestureReognizer.delegate = self ; oneTapGestureReognizer.numberOfTapsRequired = 1; //触摸次数 [ self .view addGestureRecognizer:oneTapGestureReognizer]; |
响应事件:
1 2 3 4 | -( void )oneTapGestureRecognizer:(UITapGestureRecognizer *)tapGestureRecognizer{ UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:@ "单指单击" message:@ "iOS技术交流群:228407086" delegate: self cancelButtonTitle:@ "确定" otherButtonTitles:@ "取消" , nil ]; [alertView show]; } |
效果如下:
单击手势显得稍微有点弱,我们可以继续修改手指和触摸的次数,来个双指双击看下代码:
1 2 3 4 5 | UITapGestureRecognizer *twoTapGestureReognizer = [[UITapGestureRecognizer alloc] initWithTarget: self action: @selector (twoTapGestureRecognizer:)]; twoTapGestureReognizer.delegate = self ; twoTapGestureReognizer.numberOfTouchesRequired =2; //手指数 twoTapGestureReognizer.numberOfTapsRequired=2; //触摸次数 [ self .view addGestureRecognizer:twoTapGestureReognizer]; |
响应事件如下:
1 2 3 4 | -( void )twoTapGestureRecognizer:(UITapGestureRecognizer *)tapGestureRecognizer{ UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:@ "手势点击" message:@ "iOS技术交流群:228407086" delegate: self cancelButtonTitle:@ "确定" otherButtonTitles:@ "取消" , nil ]; [alertView show]; } |
效果就不需要截图了,基本上UITapGestureRecognizer点击差不多就是设置一下手指数量和触摸次数,不过有的时候会出现同一个View上需要手势,按钮需要点击,就是事件被覆盖,需要通过UIGestureRecognizerDelegate中的方法防止事件覆盖。
1 2 3 4 5 6 7 | -( BOOL )gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{ //设置为NO则不响应 if ([ NSStringFromClass ([touch.view class ]) isEqualToString:@ "UILabel" ]) { return NO ; } return YES ; } |
上面的代码是为了让截图上的标签不响应触摸的事件,标签其实默认的是没有点击响应事件的,我们可以在标签上面加入触摸事件:
1 2 3 4 5 | UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(10, 44, 380, 30)]; label.text=@ "原文地址:http://www.cnblogs.com/xiaofeixiang" ; [label setUserInteractionEnabled: YES ]; [label addGestureRecognizer:tapGestureRecognizer]; [ self .view addSubview:label]; |
响应事件:
1 2 3 4 5 | -( void )tapJumpLink:(UITapGestureRecognizer *)tapGestureRecognizer{ UILabel *label=(UILabel *)tapGestureRecognizer.view; NSURL *url=[[ NSURL alloc]initWithString:[label.text substringFromIndex:5]]; [[UIApplication sharedApplication] openURL:url]; } |
最终效果如下:
本文转自Fly_Elephant博客园博客,原文链接:http://www.cnblogs.com/xiaofeixiang/p/4584175.html,如需转载请自行联系原作者
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
iOS开发-16进制颜色转换
项目中经常会用到颜色转换,有的是通过十六进制转成数字转颜色,想简单的点直接通过字符串转一下,简单扩展了一下分类UIColor,代码如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 +(UIColor *)colorWithHex:( NSString *)hexColor{ return [ self colorWithHex:hexColor alpha:1.0f]; } //http://www.cnblogs.com/xiaofeixiang iOS技术交流:228407086 +(UIColor *)colorWithHex:( NSString *)hexColor alpha:( float )alpha{ //删除空格 NSString *colorStr = [[hexColor stringByTrimmingCharactersInSet:[ NSCharacterSet whitespaceAndN...
- 下一篇
Android:随笔——对页面的View进行截图
转载请标明地址 QuincySx:[http://www.jianshu.com/p/71309b2bd0e7] 我们在做项目时,往往有一个这样的需求:就是对视图的一部分进行截图然后分享出去 这个功能很简单还是简单的看代码吧 <android.support.constraint.ConstraintLayout android:id="@+id/layout_test" android:layout_width="90dp" android:layout_height="90dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.3"> <ImageView android:layou...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- CentOS7编译安装Gcc9.2.0,解决mysql等软件编译问题
- CentOS关闭SELinux安全模块
- SpringBoot2整合Thymeleaf,官方推荐html解决方案
- CentOS7安装Docker,走上虚拟化容器引擎之路
- CentOS8,CentOS7,CentOS6编译安装Redis5.0.7
- Jdk安装(Linux,MacOS,Windows),包含三大操作系统的最全安装
- CentOS7,CentOS8安装Elasticsearch6.8.6
- CentOS7设置SWAP分区,小内存服务器的救世主
- Red5直播服务器,属于Java语言的直播服务器
- Docker快速安装Oracle11G,搭建oracle11g学习环境