UIGestureRecognizer有许多子类,用于监听一些常见的手势事件,这些子类主要有:

1、首先创建几个view,来用于手势的检测

  1. let view1 = UIView()
  2. let view2 = UIView()
  3. let view3 = UIView()
  4. let view4 = UIView()
  5. let view5 = UIView()
  6. let view6 = UIView()
  7. let view7 = UIView()
  8. let view8 = UIView()
  9. view1.backgroundColor = UIColor.red
  10. view2.backgroundColor = UIColor.yellow
  11. view3.backgroundColor = UIColor.blue
  12. view4.backgroundColor = UIColor.brown
  13. view5.backgroundColor = UIColor.red
  14. view6.backgroundColor = UIColor.yellow
  15. view7.backgroundColor = UIColor.blue
  16. view8.backgroundColor = UIColor.brown
  17.  
  18. view1.frame = CGRect(x:self.view.bounds.width/-,y:,width:,height:)
  19. self.view.addSubview(view1)
  20.  
  21. view2.frame = CGRect(x:self.view.bounds.width/-,y:++,width:,height:)
  22. self.view.addSubview(view2)
  23.  
  24. view3.frame = CGRect(x:self.view.bounds.width/-,y:+*+*,width:,height:)
  25. self.view.addSubview(view3)
  26.  
  27. view4.frame = CGRect(x:self.view.bounds.width/-,y:+*+*,width:,height:)
  28. self.view.addSubview(view4)
  29.  
  30. view5.frame = CGRect(x:self.view.bounds.width/+,y:,width:,height:)
  31. self.view.addSubview(view5)
  32.  
  33. view6.frame = CGRect(x:self.view.bounds.width/+,y:++,width:,height:)
  34. self.view.addSubview(view6)
  35.  
  36. view7.frame = CGRect(x:self.view.bounds.width/+,y:+*+*,width:,height:)
  37. self.view.addSubview(view7)
  38.  
  39. view8.frame = CGRect(x:self.view.bounds.width/+,y:+*+*,width:,height:)
  40. self.view.addSubview(view8)

2、滑动手势(快速滑动),可以添加多个手势绑定同一事件

  1. //滑动手势
    let swipeup = UISwipeGestureRecognizer()
    swipeup.addTarget(self, action: #selector(hswipe(recognizer:)))
    swipeup.direction = .up
    view1.addGestureRecognizer(swipeup)
            
    let swipedown = UISwipeGestureRecognizer()
    swipedown.addTarget(self, action: #selector(hswipe(recognizer:)))
    swipedown.direction = .down
    view1.addGestureRecognizer(swipedown)
            
    let swipeleft = UISwipeGestureRecognizer()
    swipeleft.addTarget(self, action: #selector(hswipe(recognizer:)))
    swipeleft.direction = .left
    view1.addGestureRecognizer(swipeleft)
            
    let swiperight = UISwipeGestureRecognizer()
    swiperight.addTarget(self, action: #selector(hswipe(recognizer:)))
    swiperight.direction = .right
    view1.addGestureRecognizer(swiperight)

实现方法:

  1. func hswipe(recognizer:UISwipeGestureRecognizer) {
            print("swipe ok")
            
            if recognizer.direction == .up {
                print("向上滑动")
            }else if recognizer.direction == .down
            {
                print("向下滑动")
            }
            
            let point = recognizer.location(in: view1)
            
            print("\(point.x)--\(point.y)")
            
            self.navigationController?.pushViewController(mySecondViewController(), animated: true)
            
        }

3、轻点手势(点击)

(1)可以通过numberOfTouchesRequired属性设置触摸点数,比如设置2表示必须两个手指触摸时才会触发

(2)通过numberOfTapsRequired属性设置点击次数,单击设置为1,双击设置为2

(3)如果一个控件既监听了单击事件也监听了双击事件,默认当双击事件触发的时候也同时会触发单击事件。

如果想双击时不触发单击,需要通过     requireGestureRecognizerToFail进行设置

  1. //单击监听
    let tapsingle = UITapGestureRecognizer()
    tapsingle.addTarget(self, action: #selector(hsswipe1))
    tapsingle.numberOfTapsRequired = 1
    tapsingle.numberOfTouchesRequired = 1
    view3.addGestureRecognizer(tapsingle)
  2.  
  3. //双击手势
    let tapDouble = UITapGestureRecognizer()
    tapDouble.addTarget(self, action: #selector(hswipe2(recon:)))
    tapDouble.numberOfTapsRequired = 2
    tapDouble.numberOfTouchesRequired = 1
    view4.addGestureRecognizer(tapDouble)

实现方法:

  1. func hsswipe1()
        {
            print("点击")
            self.navigationController?.pushViewController(mySecondViewController(), animated: true)
        }
        
        func hswipe2(recon:UISwipeGestureRecognizer)
        {
            print("双击")
            
        }

4、捏合手势

  1. //捏合手势
            let pinchTap = UIPinchGestureRecognizer()
            pinchTap.addTarget(self, action: #selector(hswipe3(recon:)))
            view5.addGestureRecognizer(pinchTap)

实现方法:

  1. func hswipe3(recon:UIPinchGestureRecognizer)
        {
            print(recon.scale)
        }

5、旋转手势

  1. //旋转手势
    let rotation = UIRotationGestureRecognizer()
    rotation.addTarget(self, action: #selector(hswipe4(recon:)))
    view6.addGestureRecognizer(rotation)

实现方法:

  1. func hswipe4(recon:UIRotationGestureRecognizer)
    {
        print(recon.rotation)
    }

6、拖动手势

  1. //拖动手势
  2. let panGap = UIPanGestureRecognizer()
  3. panGap.addTarget(self, action: #selector(hswipe5(recon:)))
  4. panGap.maximumNumberOfTouches =
  5. view7.addGestureRecognizer(panGap)

实现方法:

  1. func hswipe5(recon:UISwipeGestureRecognizer)
    {
        let point = recon.location(in: view7)
            
        view7.center = point
    }

7、长按手势

  1. //长按手势
    let longGap = UILongPressGestureRecognizer()
    longGap.addTarget(self, action: #selector(hswipe6(recon:)))
    view8.addGestureRecognizer(longGap)

实现方法:

  1. func hswipe6(recon:UISwipeGestureRecognizer)
    {
        if recon.state == .began {
         print("长按响应开始")
      }else
        {
            print("长按响应结束")
        }
     }

8、从左侧滑动

  1. //从左侧边缘划算
  2. let swipe1 = UIScreenEdgePanGestureRecognizer()
  3. swipe1.addTarget(self, action: #selector(haswipe(recon:)))
  4. swipe1.edges = .left
  5. view2.addGestureRecognizer(swipe1)

实现方法:

  1. func haswipe(recon:UISwipeGestureRecognizer)
  2. {
  3. print("从左边边缘开始滑动")
  4.  
  5. let point = recon.location(in: view2)
  6.  
  7. print("\(point.x)--\(point.y)")
  8.  
  9. }

swift - 各种手势用法大全的更多相关文章

  1. Swift - 各种手势检测大全(UIGestureRecognizer及其子类)

    UIGestureRecognizer有许多子类,用于监听一些常见的手势事件,这些子类主要有: 1,UISwipeGestureRecognizer:滑动(快速移动) 1 2 3 4 5 6 7 8 ...

  2. 转帖: 一份超全超详细的 ADB 用法大全

    增加一句 连接 网易mumu模拟器的方法 adb  connect 127.0.0.1:7555 一份超全超详细的 ADB 用法大全 2016年08月28日 10:49:41 阅读数:35890 原文 ...

  3. swift 中手势的使用

    swift 中手势的使用 /**点击手势*/ func tapGestureDemo() { //建立手势识别器 let gesture = UITapGestureRecognizer(target ...

  4. MVC5 + EF6 + Bootstrap3 (9) HtmlHelper用法大全(下)

    文章来源:Slark.NET-博客园 http://www.cnblogs.com/slark/p/mvc5-ef6-bs3-get-started-httphelper-part2.html 上一节 ...

  5. MVC5 + EF6 + Bootstrap3 (8) HtmlHelper用法大全(上)

    文章来源:Slark.NET-博客园 http://www.cnblogs.com/slark/p/mvc5-ef6-bs3-get-started-httphelper-part1.html 上一节 ...

  6. MVC HtmlHelper用法大全

    MVC HtmlHelper用法大全HtmlHelper用来在视图中呈现 HTML 控件.以下列表显示了当前可用的一些 HTML 帮助器. 本主题演示所列出的带有星号 (*) 的帮助器. ·Actio ...

  7. C# MessageBox 用法大全(转)

    C# MessageBox 用法大全 http://www.cnblogs.com/Tammie/archive/2011/08/05/2128623.html 我们在程序中经常会用到MessageB ...

  8. MVC中HtmlHelper用法大全参考

    MVC中HtmlHelper用法大全参考 解析MVC中HtmlHelper控件7个大类中各个控件的主要使用方法(1) 2012-02-27 16:25 HtmlHelper类在命令System.Web ...

  9. pandas用法大全

    pandas用法大全 一.生成数据表 1.首先导入pandas库,一般都会用到numpy库,所以我们先导入备用: import numpy as np import pandas as pd12 2. ...

随机推荐

  1. [转]C#通过Http发送Soap请求

    /// <summary>        /// 发送SOAP请求,并返回响应xml        /// </summary>        /// <param na ...

  2. u-boot编译

    u-boot目的:启动内核1.从flash读出内核,放到SDRAM中2.启动内核 u-boot要实现的功能:-----硬件相关的初始化----  ---开发的功能---------    关看门够   ...

  3. maven引入ojdbc

    http://blog.csdn.net/linminqin/article/details/17922413 ******************************************** ...

  4. 在Eclipse中给JRE-Library添加本地Javadoc

    Eclipse中的JRE-Library的Javadoc默认是一个URL,指向oracle的一个web-page,那你在离线的时候就无法使用了,为了解决这个问题,你可以从oracle下载JDK-Spe ...

  5. JVM致命错误日志(hs_err_pid.log)分析(转载)

    当jvm出现致命错误时,会生成一个错误文件 hs_err_pid<pid>.log,其中包括了导致jvm crash的重要信息,可以通过分析该文件定位到导致crash的根源,从而改善以保证 ...

  6. AIX逻辑卷扩展空间

    aix磁盘系统 a.基本概念: PV 物理卷:普通的直接访问的存储设备,有固定的和可移动的之分,代表性的就是硬盘. vg 卷组:AIX中最大的存储单位,一个卷组由一组物理硬盘组成,也就是由一个或多个物 ...

  7. PowerShell中实现人机交互

    编写脚本的过程中有很多时候需要进行人机交互,比如我写一个脚本,需要动态的输入一些内容,比如用户名和密码之类的东西,这些是没办法事先写进代码里的.而通过外部文件进行信息读取,友好性又差了点.所以当我们需 ...

  8. JavaScript高级 面向对象(6)--值类型与引用类型的存储特征

    说明(2017.3.31): 1. 画图: var num = 123; var num2 = num; 值类型赋值的存储特点:将变量num内的数据全部拷贝一份,存储给新的变量num2,内存中有2个数 ...

  9. javascript publish/subscribe or observer pattern

     定义 定义一对多的对象封装,目标对象状态发生变化,它所有的接受者都会收到通知并做相应的更新. 使用频率:5/5 最高 概要 观察者模式,也就是发布者/订阅者模式,当发布者发布一个通知的时候,订阅者就 ...

  10. [Django学习]Ajax访问静态页面

    Web开发中常用的一种开发方式是:通过Ajax进行系统的交互,采用Ajax进行交互的时候,更多的时候传输的是JSON格式的数据. 所以开发中我们需要将数据格式转化成JSON,请参见:https://w ...