iOS - UIGestureRecognizer

简介: 前言 NS_CLASS_AVAILABLE_IOS(3_2) @interface UIGestureRecognizer : NSObject @available(iOS 3.2, *) public class UIGestureRecognizer : NSOb...

前言

    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开发
如何使用iOS手势UIGestureRecognizer
如何使用iOS手势UIGestureRecognizer
100 0
|
iOS开发
iOS开发中的手势体系——UIGestureRecognizer分析及其子类的使用(一)
iOS开发中的手势体系——UIGestureRecognizer分析及其子类的使用
233 0
iOS开发中的手势体系——UIGestureRecognizer分析及其子类的使用(一)
|
iOS开发
iOS开发中的手势体系——UIGestureRecognizer分析及其子类的使用(二)
iOS开发中的手势体系——UIGestureRecognizer分析及其子类的使用
349 0
|
7天前
|
IDE Android开发 iOS开发
探索Android与iOS开发的差异:平台选择对项目成功的影响
【9月更文挑战第27天】在移动应用开发的世界中,Android和iOS是两个主要的操作系统平台。每个系统都有其独特的开发环境、工具和用户群体。本文将深入探讨这两个平台的关键差异点,并分析这些差异如何影响应用的性能、用户体验和最终的市场表现。通过对比分析,我们将揭示选择正确的开发平台对于确保项目成功的重要作用。
|
16天前
|
开发框架 数据可视化 Java
iOS开发-SwiftUI简介
iOS开发-SwiftUI简介
|
4天前
|
开发框架 移动开发 Android开发
安卓与iOS开发中的跨平台解决方案:Flutter入门
【9月更文挑战第30天】在移动应用开发的广阔舞台上,安卓和iOS两大操作系统各自占据半壁江山。开发者们常常面临着选择:是专注于单一平台深耕细作,还是寻找一种能够横跨两大系统的开发方案?Flutter,作为一种新兴的跨平台UI工具包,正以其现代、响应式的特点赢得开发者的青睐。本文将带你一探究竟,从Flutter的基础概念到实战应用,深入浅出地介绍这一技术的魅力所在。
22 7
|
7天前
|
开发框架 前端开发 Android开发
安卓与iOS开发中的跨平台解决方案
【9月更文挑战第27天】在移动应用开发的广阔天地中,安卓和iOS两大操作系统如同双子星座般耀眼。开发者们在这两大平台上追逐着创新的梦想,却也面临着选择的难题。如何在保持高效的同时,实现跨平台的开发?本文将带你探索跨平台开发的魅力所在,揭示其背后的技术原理,并通过实际案例展示其应用场景。无论你是安卓的忠实拥趸,还是iOS的狂热粉丝,这篇文章都将为你打开一扇通往跨平台开发新世界的大门。
|
13天前
|
前端开发 iOS开发 开发者
探索iOS开发中的SwiftUI框架
【9月更文挑战第21天】在iOS应用开发的广阔天地中,SwiftUI框架如一股清新之风,为开发者带来了声明式语法的便捷与高效。本文将深入探讨SwiftUI的核心概念、布局方式及数据绑定机制,同时通过实例演示如何运用SwiftUI构建用户界面,旨在引领读者领略SwiftUI的魅力,并激发其对iOS开发新趋势的思考与实践。
33 6