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

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

let view1 = UIView()
let view2 = UIView()
let view3 = UIView()
let view4 = UIView()
let view5 = UIView()
let view6 = UIView()
let view7 = UIView()
let view8 = UIView()
view1.backgroundColor = UIColor.red
view2.backgroundColor = UIColor.yellow
view3.backgroundColor = UIColor.blue
view4.backgroundColor = UIColor.brown
view5.backgroundColor = UIColor.red
view6.backgroundColor = UIColor.yellow
view7.backgroundColor = UIColor.blue
view8.backgroundColor = UIColor.brown view1.frame = CGRect(x:self.view.bounds.width/-,y:,width:,height:)
self.view.addSubview(view1) view2.frame = CGRect(x:self.view.bounds.width/-,y:++,width:,height:)
self.view.addSubview(view2) view3.frame = CGRect(x:self.view.bounds.width/-,y:+*+*,width:,height:)
self.view.addSubview(view3) view4.frame = CGRect(x:self.view.bounds.width/-,y:+*+*,width:,height:)
self.view.addSubview(view4) view5.frame = CGRect(x:self.view.bounds.width/+,y:,width:,height:)
self.view.addSubview(view5) view6.frame = CGRect(x:self.view.bounds.width/+,y:++,width:,height:)
self.view.addSubview(view6) view7.frame = CGRect(x:self.view.bounds.width/+,y:+*+*,width:,height:)
self.view.addSubview(view7) view8.frame = CGRect(x:self.view.bounds.width/+,y:+*+*,width:,height:)
self.view.addSubview(view8)

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

//滑动手势
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)

实现方法:

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进行设置

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

实现方法:

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

4、捏合手势

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

实现方法:

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

5、旋转手势

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

实现方法:

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

6、拖动手势

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

实现方法:

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

7、长按手势

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

实现方法:

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

8、从左侧滑动

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

实现方法:

func haswipe(recon:UISwipeGestureRecognizer)
{
print("从左边边缘开始滑动") let point = recon.location(in: view2) print("\(point.x)--\(point.y)") }

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. python 字符串 大小写转换 以及一系列字符串操作技巧

    总结 capitalize() 首字母大写,其余全部小写 upper() 全转换成大写 lower() 全转换成小写 title() 标题首字大写,如"i love python" ...

  2. 面试-SizeOf一个对象会得到什么?

    相关资料: 1.https://zhidao.baidu.com/question/283124338.html unit Unit1; interface uses Windows, Message ...

  3. ios开发之修改 UITableview 滚动条颜色的方法

    UITableview 的滚动条默认颜色是黑色的,如果 UItableview 背景也是深颜色,则滚动条会变的很不明显.您可以用下面这行代码来改变滚动条的颜色 self.tableView.indic ...

  4. JAVA面试题集---数据库方面_

    1.存储过程和函数的区别存储过程是用户定义的一系列sql语句的集合,涉及特定表或其它对象的任务,用户可以调用存储过程,而函数通常是数据库已定义的方法,它接收参数并返回某种类型的值并且不涉及特定用户表. ...

  5. udev规则以及编写

    主要内容: udev简介 如何配置和使用udev 如何编写udev规则 字符串替换和匹配 udev主要作用 编写udev规则实例 难点解析 1. udev简介 1.1 什么是udev? udev是Li ...

  6. Portal故障定位思路

  7. Kafka_Zookeeper_环境搭建使用

    Kafka 安装和测试 1. 简介 kafka (官网地址:http://kafka.apache.org)是一款分布式消息发布和订阅的系统,具有高性能和高吞吐率. i. 消息的发布(publish) ...

  8. [LintCode]转换字符串到整数

    问题描述: 实现atoi这个函数,将一个字符串转换为整数.如果没有合法的整数,返回0.如果整数超出了32位整数的范围,返回INT_MAX(2147483647)如果是正整数,或者INT_MIN(-21 ...

  9. CSS(一):CSS简介和基本语法

    一.CSS简介 1.什么是CSS CSS:Cascading Style Sheet:即层叠样式表.样式定义了如何显示HTML或XHTML元素.包括对字体.颜色.边距.高度.宽度.背景图片.网页定位等 ...

  10. C#里面的三种定时计时器:Timer

    在.NET中有三种计时器:1.System.Windows.Forms命名空间下的Timer控件,它直接继承自Componet.Timer控件只有绑定了Tick事件和设置Enabled=True后才会 ...