UIGestureRecognizer

UIGestureRecognizer类,用于检测、识别用户使用设备时所用的手势.它是一个抽象类,定义了所有手势的基本行为.以下是UIGestureRecognizer子类,用于处理具体的用户手势行为:

UITapGestureRecognizer // 1.单击

UILongPressGestureRecognizer // 3.长按

UISwipeGestureRecognizer // 4.轻扫

UIPanGestureRecognizer // 5.移动

UIRotationGestureRecognizer // 6.旋转

UIPinchGestureRecognizer // 7.捏合

创建手势:
    // 1.单击
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
[imgView addGestureRecognizer:tap]; // 2.双击
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTapAction:)];
doubleTap.numberOfTapsRequired = 2;
[imgView addGestureRecognizer:doubleTap]; // 双击失败才单击
[tap requireGestureRecognizerToFail:doubleTap]; // 3.长按
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
//设置最短时间
longPress.minimumPressDuration = 1;
[imgView addGestureRecognizer:longPress]; // 4.轻扫
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];
// 设置轻扫方向
[swipe setDirection:UISwipeGestureRecognizerDirectionRight];
[imgView addGestureRecognizer:swipe]; // 5.移动
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
[imgView addGestureRecognizer:pan]; // 轻扫失败才移动
[pan requireGestureRecognizerToFail:swipe]; // 6.旋转
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];
[imgView addGestureRecognizer:rotation]; // 7.捏合
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];
[imgView addGestureRecognizer:pinch];
手势触发事件:

GestureAction:

-(void)longPressAction:(UILongPressGestureRecognizer *)longPress
{
if (longPress.state == UIGestureRecognizerStateBegan) {
NSLog(@"长按开始");
}else if (longPress.state == UIGestureRecognizerStateEnded){
NSLog(@"长按结束");
}
} - (void)panAction:(UIPanGestureRecognizer *)pan { //手指所在的坐标
CGPoint point = [pan locationInView:self.view];
_view.center = point;
} - (void)rotationAction:(UIRotationGestureRecognizer *)rotation
{ if (rotation.state == UIGestureRecognizerStateChanged) { //取到弧度
CGFloat angle = rotation.rotation; //正在旋转
rotation.view.transform = CGAffineTransformMakeRotation(angle); } else if (rotation.state == UIGestureRecognizerStateEnded) { //还原
[UIView animateWithDuration:.5 animations:^{ rotation.view.transform = CGAffineTransformIdentity;
}];
}
} - (void)pinchAction:(UIPinchGestureRecognizer *)pinch
{ if (pinch.state == UIGestureRecognizerStateChanged) { // 取到缩放比率
CGFloat scale = pinch.scale; // 缩放
pinch.view.transform = CGAffineTransformMakeScale(scale, scale); } else if (pinch.state == UIGestureRecognizerStateEnded) { [UIView animateWithDuration:.5 animations:^{ pinch.view.transform = CGAffineTransformIdentity;
}];
}
}
Motion 摇晃手势
//让当前对象成为第一响应者
- (BOOL)canBecomeFirstResponder
{ return YES;
} - (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{ NSLog(@"摇一摇开始");
} - (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{ NSLog(@"摇一摇结束");
}

推荐一篇iOS手势识别的详细使用的文章:iOS手势识别


iOS手势识别器的更多相关文章

  1. iOS 手势识别器概述

    手势识别器 iOS 手势识别器(UIGestureRecognizer) 点击手势(UITapGestureRecognizer) 滑动手势(UISwipeGestureRecognizer) 旋转手 ...

  2. iOS 手势识别器(UIGestureRecognizer)

    UIGestureRecognizer是一个抽象类,定义了所有手势的基本行为,使用它的子类才能处理具体的手势. UIGestureRecognizer的子类有: UITapGestureRecogni ...

  3. iOS 七大手势之轻拍,长按,旋转手势识别器方法

    一.监听触摸事件的做法   如果想监听一个view上面的触摸事件,之前的做法通常是:先自定义一个view,然后再实现view的touches方法,在方法内部实现具体处理代码 通过touches方法监听 ...

  4. 我的IOS学习之路(三):手势识别器

    在iOS的学习中,对于手势的处理是极为重要的,如对于图片,我们经常需要进行旋转,缩放以及移动等.这里做一下总结,详见代码. - (void)viewDidLoad { [super viewDidLo ...

  5. iOS 触摸事件与手势识别器(Gesture Recognizers)

    Gesture Recognizers与触摸事件分发 通过一个问题引出今天的知识: 1.大家应该都遇见过 当需要给tableView 添加一个tap 手势识别 但是tableView 的上的事件(滑动 ...

  6. IOS 响应者链条 and UIGestureRecognizer 手势识别器)

    一次完整的触摸事件的传递响应的过程 UIAppliction --> UIWiondw -->递归找到最适合处理事件的控件 控件调用touches方法-->判断是否实现touches ...

  7. iOS 七大手势之轻拍,长按,旋转手势识别器方法-赵小波

    一.监听触摸事件的做法 如果想监听一个view上面的触摸事件,之前的做法通常是:先自定义一个view,然后再实现view的touches方法,在方法内部实现具体处理代码 通过touches方法监听vi ...

  8. iOS开发UI高级手势识别器

    ####手势识别器 UIGestureRecognizer类 ·UITapGestureRecognizer(轻击) ·UIPinchGestureRecognizer(捏合) ·UIPanGestu ...

  9. iOS的触摸事件的用法以及和手势识别器的区别

    1.首先来介绍下触摸事件和手势识别器的利与弊 触摸事件和手势识别器二者之间有直接的关系 手势识别器是在触摸事件的基础上演变过来的 当我们用到触摸事件时 默认的uiview是没有什么效果的 只能自定义v ...

随机推荐

  1. EditText的setInputType方法里面应该填什么?

    转自CSDN:http://blog.csdn.net/u014158743/article/details/52488010 | 以密码类型(password)为例 android:inputTyp ...

  2. redis在linux设置密码

    redis默认是没有密码的,如果需要设置可以这样设置. 1.找到本机的/etc/redis.conf 文件,找到如下行 #requirepass foobared 去掉前面的密码,并自己设置密码 re ...

  3. WPF&Silverlight5 常用功能差异

    一晃从Wpf转到sl也有半年多了,总想总结一下wpf和sl的差异,今天终于下笔. 首先来个整体图: 通过上图可以发现其实sl只是使用了wpf的一小部分,只是sl依赖的freamwork有很大部分都一样 ...

  4. Python pymysql模块学习心得

    PyMySQL包含了一个纯Python的MySQL客户端的库,它的目的是用来替换MySQLdb,并且工作在CPython,PyPy和IronPython. PyMySQL官方地址:https://py ...

  5. order by注入点利用方式分析

    漏洞分析 使用sqli-lab中的lesson-52作为测试目标.关键代码为: error_reporting(0); $id=$_GET['sort']; if(isset($id)) { //lo ...

  6. 使用GDI技术创建ASP.NET验证码

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Dr ...

  7. 取多个name值相同的input里面的值

    html 中是允许多个具有相同name属性的元素的,例如服务器端读取的常规做法是: string name = Request.Params["txtName"]; 得到的将是一串 ...

  8. 使用jMeter构造逻辑上有依赖关系的一系列并发请求

    相信前端开发工程师对CSRF(Cross-site request forgery)跨站请求伪造这个概念都非常熟悉,有的时候也简写成XSRF,是一种对网站的恶意利用. 尽管听起来像跨站脚本(XSS), ...

  9. github基本概念

    github: 托管项目代码. 仓库(repository):用来存放项目的代码,每个项目对应一个仓库,多个项目则有多个仓库. 收藏(star):收藏项目的人数.收藏别人的项目方便下次查看. 复制克隆 ...

  10. Django objects.values

    values(*fields) 返回一个ValuesQuerySet —— QuerySet 的一个子类,迭代时返回字典而不是模型实例对象. 每个字典表示一个对象,键对应于模型对象的属性名称. 下面的 ...