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
125 0
|
iOS开发
iOS开发中的手势体系——UIGestureRecognizer分析及其子类的使用(一)
iOS开发中的手势体系——UIGestureRecognizer分析及其子类的使用
253 0
iOS开发中的手势体系——UIGestureRecognizer分析及其子类的使用(一)
|
iOS开发
iOS开发中的手势体系——UIGestureRecognizer分析及其子类的使用(二)
iOS开发中的手势体系——UIGestureRecognizer分析及其子类的使用
387 0
|
1月前
|
iOS开发 开发者
uniapp开发ios打包Error code = -5000 Error message: Error: certificate file(p12) import failed!报错问题如何解决
uniapp开发ios打包Error code = -5000 Error message: Error: certificate file(p12) import failed!报错问题如何解决
155 67
uniapp开发ios打包Error code = -5000 Error message: Error: certificate file(p12) import failed!报错问题如何解决
|
3月前
|
开发框架 前端开发 Android开发
安卓与iOS开发中的跨平台策略
在移动应用开发的战场上,安卓和iOS两大阵营各据一方。随着技术的演进,跨平台开发框架成为开发者的新宠,旨在实现一次编码、多平台部署的梦想。本文将探讨跨平台开发的优势与挑战,并分享实用的开发技巧,帮助开发者在安卓和iOS的世界中游刃有余。
|
13天前
|
JavaScript 搜索推荐 Android开发
【01】仿站技术之python技术,看完学会再也不用去购买收费工具了-用python扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-客户的麻将软件需要下载落地页并且要做搜索引擎推广-本文用python语言快速开发爬取落地页下载-优雅草卓伊凡
【01】仿站技术之python技术,看完学会再也不用去购买收费工具了-用python扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-客户的麻将软件需要下载落地页并且要做搜索引擎推广-本文用python语言快速开发爬取落地页下载-优雅草卓伊凡
40 8
【01】仿站技术之python技术,看完学会再也不用去购买收费工具了-用python扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-客户的麻将软件需要下载落地页并且要做搜索引擎推广-本文用python语言快速开发爬取落地页下载-优雅草卓伊凡
|
2月前
|
iOS开发 开发者 MacOS
深入探索iOS开发中的SwiftUI框架
【10月更文挑战第21天】 本文将带领读者深入了解Apple最新推出的SwiftUI框架,这一革命性的用户界面构建工具为iOS开发者提供了一种声明式、高效且直观的方式来创建复杂的用户界面。通过分析SwiftUI的核心概念、主要特性以及在实际项目中的应用示例,我们将展示如何利用SwiftUI简化UI代码,提高开发效率,并保持应用程序的高性能和响应性。无论你是iOS开发的新手还是有经验的开发者,本文都将为你提供宝贵的见解和实用的指导。
152 66
|
2天前
|
人工智能 程序员 API
iOS|记一名 iOS 开发新手的前两次 App 审核经历
啥,这玩意也有新手保护期?
10 0

热门文章

最新文章

  • 1
    【01】噩梦终结flutter配安卓android鸿蒙harmonyOS 以及next调试环境配鸿蒙和ios真机调试环境-flutter项目安卓环境配置-gradle-agp-ndkVersion模拟器运行真机测试环境-本地环境搭建-如何快速搭建android本地运行环境-优雅草卓伊凡-很多人在这步就被难倒了
  • 2
    【03】仿站技术之python技术,看完学会再也不用去购买收费工具了-修改整体页面做好安卓下载发给客户-并且开始提交网站公安备案-作为APP下载落地页文娱产品一定要备案-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-优雅草卓伊凡
  • 3
    【02】仿站技术之python技术,看完学会再也不用去购买收费工具了-本次找了小影-感觉页面很好看-本次是爬取vue需要用到Puppeteer库用node.js扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-优雅草卓伊凡
  • 4
    Cellebrite UFED 4PC 7.71 (Windows) - Android 和 iOS 移动设备取证软件
  • 5
    【01】仿站技术之python技术,看完学会再也不用去购买收费工具了-用python扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-客户的麻将软件需要下载落地页并且要做搜索引擎推广-本文用python语言快速开发爬取落地页下载-优雅草卓伊凡
  • 6
    iOS各个证书生成细节
  • 7
    iOS|记一名 iOS 开发新手的前两次 App 审核经历
  • 8
    解决 input 输入框在 iOS 系统中无法输入内容
  • 9
    iOS:应用程序的线程安全性
  • 10
    iOS开发-UIScrollView原理
  • 1
    iOS|记一名 iOS 开发新手的前两次 App 审核经历
    10
  • 2
    iOS各个证书生成细节
    23
  • 3
    【01】噩梦终结flutter配安卓android鸿蒙harmonyOS 以及next调试环境配鸿蒙和ios真机调试环境-flutter项目安卓环境配置-gradle-agp-ndkVersion模拟器运行真机测试环境-本地环境搭建-如何快速搭建android本地运行环境-优雅草卓伊凡-很多人在这步就被难倒了
    122
  • 4
    Cellebrite UFED 4PC 7.71 (Windows) - Android 和 iOS 移动设备取证软件
    40
  • 5
    【03】仿站技术之python技术,看完学会再也不用去购买收费工具了-修改整体页面做好安卓下载发给客户-并且开始提交网站公安备案-作为APP下载落地页文娱产品一定要备案-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-优雅草卓伊凡
    55
  • 6
    【02】仿站技术之python技术,看完学会再也不用去购买收费工具了-本次找了小影-感觉页面很好看-本次是爬取vue需要用到Puppeteer库用node.js扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-优雅草卓伊凡
    46
  • 7
    【01】仿站技术之python技术,看完学会再也不用去购买收费工具了-用python扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-客户的麻将软件需要下载落地页并且要做搜索引擎推广-本文用python语言快速开发爬取落地页下载-优雅草卓伊凡
    40
  • 8
    uniapp开发ios打包Error code = -5000 Error message: Error: certificate file(p12) import failed!报错问题如何解决
    155
  • 9
    【05】2025年1月首发完整版-篇幅较长-苹果app如何上架到app store完整流程·不借助第三方上架工具的情况下无需花钱但需仔细学习-优雅草央千澈详解关于APP签名以及分发-们最关心的一篇来了-IOS上架app
    312
  • 10
    app开发之安卓Android+苹果ios打包所有权限对应解释列表【长期更新】-以及默认打包自动添加权限列表和简化后的基本打包权限列表以uniapp为例-优雅草央千澈
    107