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. 《Visual C++ 2010入门教程》系列五:合理组织项目、使用外部工具让工作更有效

    原文:http://www.cnblogs.com/Mrt-02/archive/2011/07/24/2115631.html 这一章跟大家分享一些与c++项目管理.VAX.SVN.VS快捷键等方面 ...

  2. 【Python】raw转义字符

    r"hi" 这里字符串前面加了r,是raw的意思,它表示对字符串不进行转义.为什么要加这个?你可以试试print "\bhi"和r"\bhi" ...

  3. Unity导出APk出错解决方法二

    错误提示(需得打开编辑器log文件才能看到全部log,Unity3d只显示一部分): Error building Player: CommandInvokationFailure: Unable t ...

  4. Spring @Value SpEl 知识点小记

    在JavaBean文件中使用Spring的@Value注解获取配置文件.yml或资源文件.properties中 key - value 键值信息 @Value("${stu.number} ...

  5. 程序员装B指南(转载)

    转自:http://www.oschina.net/question/615783_115390 一.准备工作 "工欲善其事必先利其器." 1.电脑不一定要配置高,但是双屏是必须的 ...

  6. angular自定义指令解决IE89不支持input的placeholder属性

    下面代码实测通过,直接copy到本地运行即可. <!DOCTYPE html> <html> <head> <meta charset="UTF-8 ...

  7. 个体商户POS机遭遇禁刷 职业养卡人称自有对策

    “套现猛于虎也”,这对于信用卡业而言无异于一大命门,信用卡套现金额的规模如同滚雪球般愈演愈烈.记者昨日采访银行业内了解到,虽然为防套现将根据规定关闭个体商户POS机刷信用卡的功能,但职业“养卡人”不以 ...

  8. mysql中replicate_wild_do_table和replicate_do_db区别

    使用replicate_do_db和replicate_ignore_db时有一个隐患,跨库更新时会出错. 如在Master(主)服务器上设置 replicate_do_db=test(my.conf ...

  9. Python初学者第七天 字符串及简单操作

    7day 数据类型:字符串 1.定义 字符串是一个有序的字符的集合,用于储存和表示基本的文本信息.单.双.三引号之间的内容称之为字符串: a = ‘hello world!’ b = "你好 ...

  10. SqlParameter.Value = NULL 引发的数据库异常

    摘自:http://www.cnblogs.com/ccweb/p/3403492.html using (SqlCommand cmd = new SqlCommand()) { cmd.Conne ...