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. push本地代码到github发生错误的解决办法

    有一天,我在github上编写好了README.md这个文件,接着发现本地有段代码忘记上传了,于是熟练的输入git add .   git commit -m "modify",以 ...

  2. 微信小程序——组件(二)

    在上篇文章组件(一)里已经讲解了如何创建一个项目,现在继续...讲解一个页面布局以及各个组件的使用.在学习过程中,发现小程序支持flex布局,这对于学习过react native的人来说太好了,布局方 ...

  3. 使用intellij idea搭建MAVEN+SSM(Spring+SpringMVC+MyBatis)框架

    基本概念 使用SSM(Spring,SpringMVC和Mybatis) 1.1.Spring Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod  ...

  4. 修改virtual box中ubuntu lubuntu 的分辨率

    Step1 先用xrandr命令查看能够支持的分辨率 Step2 xrandr --output VGA-1 --size 1280x800 Step3 重启电脑

  5. servlet的使用

    Servlet是比较基础的的客户端与服务器数据交互程序,通过HttpServletRequest请求和HttpServletResponse响应,可以基本实现web程序开发. 1.Servlet基础代 ...

  6. JDBC(2)Statement

    Statement: 用于执行SQL语句的对象 通过Connection的createStatement()方法得到一个Statement对象 只有在获得了Statement对象之后才能执行SQL对象 ...

  7. Search - Dictionary

    Search III Your task is to write a program of a simple dictionary which implements the following ins ...

  8. vue中图片返回404时,显示默认的图片

    图片返回404时候的处理 <img :src="userMsg.portrait" ref="img" alt=""> _thi ...

  9. SpringAop之日志管理

    导入的依赖均为JavaWeb界面在线配置代码生成器这篇文章,你只需将这篇文章的maven依赖导入即可. SpringAop利用注解的特性进行日志管理,只需在对应的方法上加上自己编写的注解,即可完美实现 ...

  10. layUI不同页面传参数

    //页面一的方法 function modifyHotSearch(id){ layer.open({ type:2, title:"修改热门搜索", area: ['600px' ...