手势在iOS开发中是一个比较常用的功能,不过相对来说大家用的比较少,经常刷网易新闻,上次用了一下捏合手势才发现可以调整字体大小。昨天看到一个介绍摇一摇这个功能的,没看到之前一直都觉得摇一摇是微信的专有的,昨天测试了一下知乎,感觉像发现了一个新大陆,随便截了图,效果如下:

扯的有点远了,很多应用的很多功能其实对于大多数而言是没有用到的,不过作为程序员我们还是应该多研究一下。

基础概念

常见的手势有六种,如下图所示:

UITapGestureRecognizer(点击,轻触摸)、UIPinchGestureRecognizer(二指往內或往外拨动,捏合手势)、UIPanGestureRecognizer(拖移)、UISwipeGestureRecognizer(滑动,快速移动)、UIRotationGestureRecognizer(旋转)和UILongPressGestureRecognizer(长按),由于微信的缘故应该大多数人对长按比较熟悉,Tap点击也是高频用到的手势。

苹果官方给出了Tap和Pinch的手势的效果图,其他的效果可以私下试一试:

Demo实战

由于有六种手势,基本上大同小异,其中一种会实战,其他的应该也没问题,接下来的的介绍都是以UITapGestureRecognizer为基准的,先来个简单的单击手势:

    UITapGestureRecognizer *oneTapGestureReognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(oneTapGestureRecognizer:)];
oneTapGestureReognizer.delegate = self;
oneTapGestureReognizer.numberOfTapsRequired = 1;//触摸次数
[self.view addGestureRecognizer:oneTapGestureReognizer];

响应事件:

-(void)oneTapGestureRecognizer:(UITapGestureRecognizer *)tapGestureRecognizer{
UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:@"单指单击" message:@"iOS技术交流群:228407086" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
[alertView show];
}

效果如下:

单击手势显得稍微有点弱,我们可以继续修改手指和触摸的次数,来个双指双击看下代码:

  UITapGestureRecognizer *twoTapGestureReognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(twoTapGestureRecognizer:)];
twoTapGestureReognizer.delegate = self;
twoTapGestureReognizer.numberOfTouchesRequired =2;//手指数
twoTapGestureReognizer.numberOfTapsRequired=2;//触摸次数
[self.view addGestureRecognizer:twoTapGestureReognizer];

响应事件如下:

-(void)twoTapGestureRecognizer:(UITapGestureRecognizer *)tapGestureRecognizer{
UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:@"手势点击" message:@"iOS技术交流群:228407086" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
[alertView show];
}

效果就不需要截图了,基本上UITapGestureRecognizer点击差不多就是设置一下手指数量和触摸次数,不过有的时候会出现同一个View上需要手势,按钮需要点击,就是事件被覆盖,需要通过UIGestureRecognizerDelegate中的方法防止事件覆盖。

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
//设置为NO则不响应
if ([NSStringFromClass([touch.view class]) isEqualToString:@"UILabel"]) {
return NO;
}
return YES;
}

上面的代码是为了让截图上的标签不响应触摸的事件,标签其实默认的是没有点击响应事件的,我们可以在标签上面加入触摸事件:

  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];

响应事件:

-(void)tapJumpLink:(UITapGestureRecognizer *)tapGestureRecognizer{
UILabel *label=(UILabel *)tapGestureRecognizer.view;
NSURL *url=[[NSURL alloc]initWithString:[label.text substringFromIndex:5]];
[[UIApplication sharedApplication] openURL:url];
}

最终效果如下:

附赠iOS技术交流群:228407086,如果关于博客或者iOS有什么问题,欢迎入群讨论~

iOS开发-UITapGestureRecognizer手势的更多相关文章

  1. 【转】 iOS开发之手势gesture详解

    原文:http://www.cnblogs.com/salam/archive/2013/04/30/iOS_gesture.html 前言 在iOS中,你可以使用系统内置的手势识别 (Gesture ...

  2. iOS开发之手势gesture详解(一)

    前言 在iOS中,你可以使用系统内置的手势识别(GestureRecognizer),也可以创建自己的手势.GestureRecognizer将低级别的转换为高级别的执行行为,是你绑定到view的对象 ...

  3. iOS开发摇动手势实现详解

    1.当设备摇动时,系统会算出加速计的值,并告知是否发生了摇动手势.系统只会运动开始和结束时通知你,并不会在运动发生的整个过程中始终向你报告每一次运动.例如,你快速摇动设备三次,那只会收到一个摇动事件. ...

  4. iOS开发之手势gesture详解(二)

    与其他用户界面控件交互 UIControl子类会覆盖parentView的gesture.例如当用户点击UIButton时,UIButton会接受触摸事件,它的parentView不会接收到.这仅适用 ...

  5. ios开发之手势处理 之手势识别一

    #import "ViewController.h" @interface ViewController ()<UIGestureRecognizerDelegate> ...

  6. iOS开发 UIPanGestureRecognizer手势抽象类

    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@sel ...

  7. iOS开发: 向右滑动手势功能实现

    在navigationController中实现向右滑动 返回功能 系统提供的backbarbuttonitem,不用添加任何代码即可实现向右滑动后退功能,但是往往要对按钮修改样式等时,就需要自定义l ...

  8. ios开发之手势动作状态细分state,同一视图加入两个手势

    1.比方拖拽一个视图.形成类似scrollView的翻页形式 在拖拽的方法里推断拖拽的状态state属性,依据状态不同运行自己须要的效果. 2.同一视图加入两个手势,须要使用手势的代理方法.同意此操作 ...

  9. iOS开发拓展篇—xib中关于拖拽手势的潜在错误

    iOS开发拓展篇—xib中关于拖拽手势的潜在错误 一.错误说明 自定义一个用来封装工具条的类 搭建xib,并添加一个拖拽的手势. 主控制器的代码:加载工具条 封装工具条以及手势拖拽的监听事件 此时运行 ...

随机推荐

  1. 数据包发包工具bittwist

    数据包发包工具bittwist   渗透测试中,通过发送特定格式的包,可以实施网络嗅探和攻击.Kali Linux提供一款发包工具bittwist.该工具可以通过指定的网络接口发送数据.该工具不仅可以 ...

  2. gson Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path

    返回数据解析错误 com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT ...

  3. makefile 必知必会以及Makefile是怎样炼成的

    Make必知必会原文链接 Makefile 必知必会 Makefile的根本任务是根据规则生成目标文件. 规则 一条规则包含三个:目标文件,目标文件依赖的文件,更新(或生成)目标文件的命令. 规则: ...

  4. OpenVPN使用easy-rsa3吊销证书

    cd /etc/easy-rsa ./easyrsa revoke targetkey(证书名) ./easyrsa gen-crl 其中gen-crl会生成一份吊销证书的名单,放在pki/crl.p ...

  5. spring-boot 速成(5) profile区分环境

    maven中的profile概念,在spring-boot中一样适合,只要约定以下几个规则即可: 一.不同环境的配置文件以"application-环境名.yml"命名 举个粟子: ...

  6. 【汇总】PHP-FPM 配置优化(转)

    -----------------------开启php-fpm慢脚本日志 request_slowlog_timeout = 30sslowlog = /usr/local/php/var/log/ ...

  7. How to convert a byte to its binary string representation

    How to convert a byte to its binary string representation For example, the bits in a byte B are 1000 ...

  8. ChromeDriver启动Chrome浏览器后,地址栏只显示data;——chromeDriver版本不对

    ChromeDriver启动Chrome浏览器后,地址栏只显示data; 错误原因: chromeDriver版本不对,不同版本的chromeDriver对应不同版本的chrome浏览器 chrome ...

  9. layer.confirm 询问框 的层遮盖

    function admin_del(obj) { layer.confirm('确认要重启吗?', { btn : [ '确定', '取消' ]//按钮 }, function(index) { l ...

  10. OPENSSL FIPS

    https://www.openssl.org/docs/fipsnotes.html https://wiki.openssl.org/index.php/FIPS_mode() openssl-f ...