前言

	NS_CLASS_AVAILABLE_IOS(3_2)  @interface UIGestureRecognizer : NSObject
@available(iOS 3.2, *) public class UIGestureRecognizer : NSObject // 点击(轻点)
NS_CLASS_AVAILABLE_IOS(3_2) @interface UITapGestureRecognizer : UIGestureRecognizer
@available(iOS 3.2, *) public class UITapGestureRecognizer : UIGestureRecognizer // 长按(按住不放)
NS_CLASS_AVAILABLE_IOS(3_2) @interface UILongPressGestureRecognizer : UIGestureRecognizer
@available(iOS 3.2, *) public class UILongPressGestureRecognizer : UIGestureRecognizer // 旋转(两个手指进行旋转)
NS_CLASS_AVAILABLE_IOS(3_2) @interface UIRotationGestureRecognizer : UIGestureRecognizer
@available(iOS 3.2, *) public class UIRotationGestureRecognizer : UIGestureRecognizer // 捏合(两个手指,缩放手势)
NS_CLASS_AVAILABLE_IOS(3_2) @interface UIPinchGestureRecognizer : UIGestureRecognizer
@available(iOS 3.2, *) public class UIPinchGestureRecognizer : UIGestureRecognizer // 拖动(移动速度较慢)
NS_CLASS_AVAILABLE_IOS(3_2) @interface UIPanGestureRecognizer : UIGestureRecognizer
@available(iOS 3.2, *) public class UIPanGestureRecognizer : UIGestureRecognizer // 滑动(快速移动)
NS_CLASS_AVAILABLE_IOS(3_2) @interface UISwipeGestureRecognizer : UIGestureRecognizer
@available(iOS 3.2, *) public class UISwipeGestureRecognizer : UIGestureRecognizer

1、tapGesture 点击手势

1.1 tapGesture 的创建

  • Objective-C

    	// 实例化点击手势对象
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self
    action:@selector(tapClick:)]; // 向 imageView 添加点击手势
    [imageView addGestureRecognizer:tapGesture];
  • Swift

    	// 实例化点击手势对象
    let tapGesture:UITapGestureRecognizer = UITapGestureRecognizer(target: self,
    action: #selector(UiGestureRecognizer.tapClick(_:))) // 向 imageView 添加点击手势
    imageView?.addGestureRecognizer(tapGesture)

1.2 tapGesture 的设置

  • Objective-C

    	// 设置点击次数
    /*
    默认为 1:单击,为 2 时为双击
    */
    singleTapGesture.numberOfTapsRequired = 1; // 设置触摸点数
    /*
    默认为 1,单个手指触摸
    */
    singleTapGesture.numberOfTouchesRequired = 1; // 单双击共存
    /*
    设置单击手势与双击手势共存,当没有检测到双击手势或检测双击手势失败时单击手势才有效
    */
    [singleTapGesture requireGestureRecognizerToFail:doubleTapGesture]; // 获取点击的视图
    /*
    剪取点击的视图,附带点击手势一起剪取
    */
    UIView *tapView = tapGesture.view; // 获取点击的图片
    /*
    复制点击的图片,附带点击手势一起复制
    */
    UIImage *tapImage = ((UIImageView *)tapGesture.view).image;
  • Swift

    	// 设置点击次数
    /*
    默认为 1:单击,为 2 时为双击
    */
    singleTapGesture.numberOfTapsRequired = 1 // 设置触摸点数
    /*
    默认为 1,单个手指触摸
    */
    singleTapGesture.numberOfTouchesRequired = 1 // 单双击共存
    /*
    设置单击手势与双击手势共存,当没有检测到双击手势或检测双击手势失败时单击手势才有效
    */
    singleTapGesture.requireGestureRecognizerToFail(doubleTapGesture) // 获取点击的视图
    /*
    剪取点击的视图,附带点击手势一起剪取
    */
    let tapView:UIView = tapGesture.view! // 获取点击的图片
    /*
    复制点击的图片,附带点击手势一起复制
    */
    let tapImage:UIImage = (tapGesture.view! as! UIImageView).image!

1.3 自定义触摸响应事件处理

  • Objective-C

    	- (void)tapClick:(UITapGestureRecognizer *)tapGesture {
    
    	}
  • Swift

    	func tapClick(tapGesture:UITapGestureRecognizer) {
    
    	}

2、longPressGesture 长按手势

2.1 longPressGesture 的创建

  • Objective-C

    	// 实例化长按手势对象
    UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self
    action:@selector(longPressClick:)]; // 向 imageView 添加长按手势
    [imageView addGestureRecognizer:longPressGesture];
  • Swift

    	// 实例化长按手势对象
    let longPressGesture:UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: self,
    action: #selector(UiGestureRecognizer.longPressClick(_:))) // 向 imageView 添加长按手势
    imageView?.addGestureRecognizer(longPressGesture)

2.2 longPressGesture 的设置

  • Objective-C

    	// 获取手势状态
    UIGestureRecognizerState state = longPressGesture.state; // 长按手势开始
    if (longPressGesture.state == UIGestureRecognizerStateBegan) { } // 长按手势触发结束
    if (longPressGesture.state == UIGestureRecognizerStateEnded) { }
  • Swift

    	// 获取手势状态
    let state:UIGestureRecognizerState = longPressGesture.state // 长按手势开始
    if longPressGesture.state == UIGestureRecognizerState.Began { } // 长按手势触发结束
    if longPressGesture.state == UIGestureRecognizerState.Ended { }

2.3 自定义触摸响应事件处理

  • Objective-C

    	- (void)longPressClick:(UILongPressGestureRecognizer *)longPressGesture {
    
    	}
  • Swift

    	func longPressClick(longPressGesture:UILongPressGestureRecognizer) {
    
    	}

3、rotationGesture 旋转手势

3.1 rotationGesture 的创建

  • Objective-C

    	// 实例化旋转手势对象
    UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self
    action:@selector(rotationClick:)]; // 向 imageView 添加旋转手势
    [imageView addGestureRecognizer:rotationGesture];
  • Swift

    	// 实例化旋转手势对象
    let rotationGesture:UIRotationGestureRecognizer = UIRotationGestureRecognizer(target: self,
    action: #selector(UiGestureRecognizer.rotationClick(_:))) // 向 imageView 添加旋转手势
    imageView?.addGestureRecognizer(rotationGesture)

3.2 rotationGesture 的设置

  • Objective-C

    	// 获取旋转角度
    /*
    rotation 获取到的为弧度,1 度 = PI/180 弧度
    */
    CGFloat rotation = rotationGesture.rotation * 180 * M_1_PI; // 图片旋转
    /*
    lastRotation 为之前的角度
    */
    imageView.transform = CGAffineTransformMakeRotation(lastRotation + rotationGesture.rotation); // 旋转手势触发结束
    if (rotationGesture.state == UIGestureRecognizerStateEnded) { lastRotation += rotationGesture.rotation;
    }
  • Swift

    	// 获取旋转角度
    /*
    rotation 获取到的为弧度,1 度 = PI/180 弧度
    */
    let rotation:CGFloat = rotationGesture.rotation * 180 * CGFloat(M_1_PI) // 图片旋转
    /*
    lastRotation 为之前的角度
    */
    imageView?.transform = CGAffineTransformMakeRotation(lastRotation + rotationGesture.rotation) // 旋转手势触发结束
    if rotationGesture.state == UIGestureRecognizerState.Ended { lastRotation += rotationGesture.rotation
    }

3.3 自定义触摸响应事件处理

  • Objective-C

    	// 用模拟器时需按住 option 键
    - (void)rotationClick:(UIRotationGestureRecognizer *)rotationGesture { }
  • Swift

    	// 用模拟器时需按住 option 键
    func rotationClick(rotationGesture:UIRotationGestureRecognizer) { }

4、pinchGesture 捏合手势

4.1 pinchGesture 的创建

  • Objective-C

    	// 实例化捏合手势对象
    UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self
    action:@selector(pinchClick:)]; // 向 imageView 添加捏合手势
    [imageView addGestureRecognizer:pinchGesture];
  • Swift

    	// 实例化捏合手势对象
    let pinchGesture:UIPinchGestureRecognizer = UIPinchGestureRecognizer(target: self,
    action: #selector(UiGestureRecognizer.pinchClick(_:))) // 向 imageView 添加捏合手势
    imageView?.addGestureRecognizer(pinchGesture)

4.2 pinchGesture 的设置

  • Objective-C

    	// 获取缩放倍数
    CGFloat scale = pinchGesture.scale; // 图片缩放
    imageView.transform = CGAffineTransformMakeScale(pinchGesture.scale, pinchGesture.scale);
    imageView.bounds = CGRectMake(0, 0, imageView.bounds.size.width * pinchGesture.scale,
    imageView.bounds.size.height * pinchGesture.scale); // 还原缩放倍数
    [pinchGesture setScale:1];
  • Swift

    	// 获取缩放倍数
    let scale:CGFloat = pinchGesture.scale // 图片缩放
    imageView?.transform = CGAffineTransformMakeScale(pinchGesture.scale, pinchGesture.scale)
    imageView?.bounds = CGRectMake(0, 0, imageView!.bounds.size.width * pinchGesture.scale,
    imageView!.bounds.size.height * pinchGesture.scale) // 还原缩放倍数
    pinchGesture.scale = 1

4.3 自定义触摸响应事件处理

  • Objective-C

    	// 用模拟器时需按住 option 键
    - (void)pinchClick:(UIPinchGestureRecognizer *)pinchGesture { }
  • Swift

    	// 用模拟器时需按住 option 键
    func pinchClick(pinchGesture:UIPinchGestureRecognizer) { }

5、panGesture 拖动手势

5.1 panGesture 的创建

  • Objective-C

    	// 实例化拖拽手势对象
    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self
    action:@selector(panClick:)]; // 向 imageView 添加拖拽手势
    [imageView addGestureRecognizer:panGesture];
  • Swift

    	// 实例化拖拽手势对象
    let panGesture:UIPanGestureRecognizer = UIPanGestureRecognizer(target: self,
    action: #selector(UiGestureRecognizer.panClick(_:))) // 向 imageView 添加拖拽手势
    imageView?.addGestureRecognizer(panGesture)

5.2 panGesture 的设置

  • Objective-C

    	// 获取手势位置
    CGPoint currentPoint = [panGesture locationInView:self.view]; // 图片移动
    imageView.center = currentPoint;
  • Swift

    	// 获取手势位置
    let currentPoint:CGPoint = panGesture.locationInView(self.view) // 图片移动
    imageView?.center = currentPoint

5.3 自定义触摸响应事件处理

  • Objective-C

    	- (void)panClick:(UIPanGestureRecognizer *)panGesture {
    
    	}
  • Swift

    	func panClick(panGesture:UIPanGestureRecognizer) {
    
    	}

6、swipeGesture 滑动手势

6.1 swipeGesture 的创建

  • Objective-C

    	// 实例化滑动手势对象
    UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self
    action:@selector(swipeClick:)]; // 设置滑动方向,默认为 0:向右滑动
    swipeGesture.direction = UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight; // 向 imageView 添加拖拽手势
    [imageView addGestureRecognizer:swipeGesture];
  • Swift

    	// 实例化滑动手势对象
    let swipeGesture:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self,
    action: #selector(UiGestureRecognizer.swipeClick(_:))) // 设置滑动方向,默认为 0:向右滑动
    swipeGesture.direction = [.Left, .Right] // 向 imageView 添加拖拽手势
    imageView?.addGestureRecognizer(swipeGesture)

6.2 swipeGesture 的设置

  • Objective-C

    	// 获取滑动方向
    UISwipeGestureRecognizerDirection direction = swipeGesture.direction;
  • Swift

    	// 获取滑动方向
    let direction:UISwipeGestureRecognizerDirection = swipeGesture.direction

6.3 自定义触摸响应事件处理

  • Objective-C

    	- (void)swipeClick:(UISwipeGestureRecognizer *)swipeGesture {
    
    	}
  • Swift

    	func swipeClick(swipeGesture:UISwipeGestureRecognizer) {
    
    	}

iOS - UIGestureRecognizer的更多相关文章

  1. iOS UIGestureRecognizer与UIMenuController(内容根据iOS编程)

    UIGestureRecognizer 对象会截取本应由视图处理的触摸事件.当某个UIGestureRecognizer对象识别出特定的手势后,就会向指定的对象发送指定的消息.iOS SDK默认提供若 ...

  2. XamarinForm Effects 调用事件

    原文地址 在Xamarin.Forms控件中实现底层多点触控跟踪. 一个effect可以定义和调用一个事件,在底层本地视图中发出信号的变化.这篇文章演示如何实现底层多点触控跟踪,以及如何生成信号触摸活 ...

  3. iOS基础篇(十七)——UIGestureRecognizer用法

    UIGestureRecognizer(手势识别)在iOS 中非常重要,他极大地提高了移动设备的使用便捷性: 在3.2之前是主要使用的是由UIResponder而来的如下4种方式: - (void)t ...

  4. 点击事件touches与ios的手势UIGestureRecognizer

    .h文件 @property (weak,nonatomic) IBOutlet UILabel *messageLabel;@property (weak,nonatomic) IBOutlet U ...

  5. UIGestureRecognizer ios手势识别温习

    1.UIGestureRecognizer介绍 手势识别在iOS上非常重要,手势操作移动设备的重要特征,极大的增加了移动设备使用便捷性. iOS系统在3.2以后,为方便开发这使用一些常用的手势,提供了 ...

  6. [BS-25] IOS中手势UIGestureRecognizer概述

    IOS中手势UIGestureRecognizer概述 一.概述 iPhone中处理触摸屏的操作,在3.2之前是主要使用的是由UIResponder而来的如下4种方式: - (void)touches ...

  7. iOS手势学习UIGestureRecognizer & cocos2d 手势推荐

    iOS手势学习UIGestureRecognizer & cocos2d 手势推荐 手势识别类型: UILongPressGestureRecognizer  // 长按UIPanGestur ...

  8. ios的手势操作之UIGestureRecognizer浅析

    转载地址:http://blog.csdn.net/likendsl/article/details/7554150 每一个手势的实现例子,可参考下面网址:http://www.cnblogs.com ...

  9. iOS手势UIGestureRecognizer的使用及手势冲突的解决办法【转】

    转自:iOS开发中的手势体系——UIGestureRecognizer分析及其子类的使用 关于手势的一篇很好的帖子,转载过来免得丢失.你可能最感兴趣的是手势间的互斥处理,那么就搜索 4.手势间的互斥处 ...

随机推荐

  1. <<构建之法>>略读感想

    经过对构建之法这本书的快速阅读和学习,我有以下疑问. 1.对软件工程来说是应该更注重结果和功能的实现还是更注重代码的易读和完整? 2.应该怎样平衡不同用户的不同需求以达到使大多数人满意的目的? 3.应 ...

  2. 杭电1071-The area

    问题描述:   Ignatius bought a land last week, but he didn't know the area of the land because the land i ...

  3. 【转】详细讲解Java中log4j的使用方法

    转载地址:http://www.233.com/Java/zhuangye/20070731/142625631.html 1.Log4j是什么? Log4j可以帮助调试(有时候debug是发挥不了作 ...

  4. 使用django的ImageField和from制作上传图片页面

    需求描述: 做一个简单的注册页面,使得用户在注册页上传头像. 解决办法: 以前用java写这个的时候,在action上面需要用IO接受文件,然后生成一个文件名,再将文件相对路径保存到user表的img ...

  5. PHP中cookie和Session

    Cookie与Session cookie 列子 if(!isset($_COOKIE['cookie'])){ setcookie("cookie",date('Y-m-d H: ...

  6. LA 3907 Puzzle

    问题描述:先给你s个禁止串,求不包含禁止串的最长串,如果存在,打印字典序最大. 数据范围:s <= 1000, 禁止串长度不超过50. 分析:不匹配问题实际上等同于匹配问题.假设我们已经有满足条 ...

  7. Area(Pick定理POJ1256)

    Area Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5429   Accepted: 2436 Description ...

  8. The shortest problem

    The shortest problem Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...

  9. Unity物理投射相关问题整理

    1.投射目标是否需要附加刚体,是否可忽略触发器? 默认既支持触发器,也支持刚体.投射的最后一个参数queryTriggerInteraction可以设置,是否包含触发器事件. 2.非射线投射,是否有接 ...

  10. JAVA基础知识之Queue集合

    Queue接口 PriorityQueue类 Deque与ArrayDeque LinkedList 各种线性表性能分析 Queue接口 Queue用来模拟队列这种数据结构,遵循先进先出原则(FIFO ...