一 Tap轻点

 // 双指轻点(需用真机测试)let doubleFingers = UITapGestureRecognizer(target: self, action: #selector(self.doubleTap(_:)))// 点几下才触发,设置1时,就是点一下触发doubleFingers.numberOfTapsRequired = 1// 几个手指触发doubleFingers.numberOfTouchesRequired = 2// 为视图加入监听手势self.view.addGestureRecognizer(doubleFingers)// 单指轻点let singleFingers = UITapGestureRecognizer(target: self, action: #selector(self.singleTap(_:)))// 点几下才触发,设置1时,就是点一下触发singleFingers.numberOfTapsRequired = 2// 几个手指触发singleFingers.numberOfTouchesRequired = 1// 双指轻点没有触发时,才会检测次手势,以免手势被盖过singleFingers.require(toFail: doubleFingers)// 为视图加入监听手势self.view.addGestureRecognizer(singleFingers)

Long Press长按

 // 长按
let longPress = UILongPressGestureRecognizer(target: self, action: #selector(ViewController.longPress(_:)))// 为视图加入监听手势self.view.addGestureRecognizer(longPress)

Swipe滑动

 /**=======Swipe滑动手势========*/// 可以移动的UIViewmyUIView = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))myUIView.backgroundColor = .blueself.view.addSubview(myUIView)// 向上滑动let swipeLeft = UISwipeGestureRecognizer(target:self, action:#selector(ViewController.swipe(_:)))swipeLeft.direction = .leftself.view.addGestureRecognizer(swipeLeft)// 向下滑动let swipeDown = UISwipeGestureRecognizer(target:self, action:#selector(ViewController.swipe(_:)))swipeDown.direction = .downself.view.addGestureRecognizer(swipeDown)// 向右滑动let swipeRight = UISwipeGestureRecognizer(target:self, action:#selector(ViewController.swipe(_:)))swipeRight.direction = .rightself.view.addGestureRecognizer(swipeRight)

Pan拖拽

 // 可以移动的UIView
anotherUIView = UIView(frame: CGRect(x: fullSize.width * 0.5, y: fullSize.height * 0.5, width: 100, height: 100))anotherUIView.backgroundColor = .orangeself.view.addSubview(anotherUIView)// 拖拽手势let pan = UIPanGestureRecognizer(target: self, action: #selector(self.pan(_:)))// 最少用几指拖拽pan.minimumNumberOfTouches = 1// 最多用几指拖拽pan.maximumNumberOfTouches = 1anotherUIView.addGestureRecognizer(pan)

完整代码:

class ViewController: UIViewController {var fullSize = UIScreen.main.bounds.sizevar myUIView: UIView!var anotherUIView: UIView!override func viewDidLoad() {super.viewDidLoad()// Do any additional setup after loading the view./**=======Tap轻点手势========*/// 双指轻点(需用真机测试)let doubleFingers = UITapGestureRecognizer(target: self, action: #selector(self.doubleTap(_:)))// 点几下才触发,设置1时,就是点一下触发doubleFingers.numberOfTapsRequired = 1// 几个手指触发doubleFingers.numberOfTouchesRequired = 2// 为视图加入监听手势self.view.addGestureRecognizer(doubleFingers)// 单指轻点let singleFingers = UITapGestureRecognizer(target: self, action: #selector(self.singleTap(_:)))// 点几下才触发,设置1时,就是点一下触发singleFingers.numberOfTapsRequired = 2// 几个手指触发singleFingers.numberOfTouchesRequired = 1// 双指轻点没有触发时,才会检测次手势,以免手势被盖过singleFingers.require(toFail: doubleFingers)// 为视图加入监听手势self.view.addGestureRecognizer(singleFingers)/**=======Tap长按手势========*/// 长按let longPress = UILongPressGestureRecognizer(target: self, action: #selector(ViewController.longPress(_:)))// 为视图加入监听手势self.view.addGestureRecognizer(longPress)/**=======Swipe滑动手势========*/// 可以移动的UIViewmyUIView = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))myUIView.backgroundColor = .blueself.view.addSubview(myUIView)// 向上滑动let swipeLeft = UISwipeGestureRecognizer(target:self, action:#selector(ViewController.swipe(_:)))swipeLeft.direction = .leftself.view.addGestureRecognizer(swipeLeft)// 向下滑动let swipeDown = UISwipeGestureRecognizer(target:self, action:#selector(ViewController.swipe(_:)))swipeDown.direction = .downself.view.addGestureRecognizer(swipeDown)// 向右滑动let swipeRight = UISwipeGestureRecognizer(target:self, action:#selector(ViewController.swipe(_:)))swipeRight.direction = .rightself.view.addGestureRecognizer(swipeRight)/**=======Pan 拖拽手势========*/// 可以移动的UIViewanotherUIView = UIView(frame: CGRect(x: fullSize.width * 0.5, y: fullSize.height * 0.5, width: 100, height: 100))anotherUIView.backgroundColor = .orangeself.view.addSubview(anotherUIView)// 拖拽手势let pan = UIPanGestureRecognizer(target: self, action: #selector(self.pan(_:)))// 最少用几指拖拽pan.minimumNumberOfTouches = 1// 最多用几指拖拽pan.maximumNumberOfTouches = 1anotherUIView.addGestureRecognizer(pan)}// 触发滑动手势后执行的动作@objc func swipe(_ recognizer:UISwipeGestureRecognizer) {let point = myUIView.centerif recognizer.direction == .up {print("Go up")if point.y >= 150 {myUIView.center = CGPoint(x: myUIView.center.x, y: myUIView.center.y - 100)} else {myUIView.center = CGPoint(x: myUIView.center.x, y: 50)}}else if recognizer.direction == .left {print("Go Left")if point.x >= 150 {myUIView.center = CGPoint(x: myUIView.center.x - 100, y: myUIView.center.y)} else {myUIView.center = CGPoint(x: 50, y: myUIView.center.y)}} else if recognizer.direction == .down {print("Go Down")if point.y <= fullSize.height - 150 {myUIView.center = CGPoint(x: myUIView.center.x, y: myUIView.center.y + 100)} else {myUIView.center = CGPoint(x: myUIView.center.x, y: fullSize.height - 50)}} else if recognizer.direction == .right {print("Go Right")if point.x <= fullSize.width - 150 {myUIView.center = CGPoint(x: myUIView.center.x + 100, y: myUIView.center.y)} else {myUIView.center = CGPoint(x: fullSize.width - 50, y: myUIView.center.y)}}}// 触发双指后执行的动作@objc func doubleTap(_ recognizer:UITapGestureRecognizer){print("双指点一下触发")// 取得每指的位置self.findFingersPositon(recognizer)}// 触发长按后执行的动作@objc func longPress(_ recognizer:UILongPressGestureRecognizer) {if recognizer.state == .began {print("长按开始")} else if recognizer.state == .ended {print("长按结束")}}// 触发单指轻点2下后执行的动作@objc func singleTap(_ recognizer:UITapGestureRecognizer){print("单指轻点2下")// 取得每指的位置self.findFingersPositon(recognizer)}// 触发拖拽后执行的动作@objc func pan(_ recognizer:UIPanGestureRecognizer) {// 设置UIView的新位置let point = recognizer.location(in: self.view)anotherUIView.center = point}func findFingersPositon(_ recognizer: UITapGestureRecognizer) {let number = recognizer.numberOfTouchesfor i in 0..<number {let point = recognizer.location(ofTouch: i, in: recognizer.view)print("第\(i+1)指的位置是:\(NSCoder.string(for: point))")}}}

点个赞再走吧

IOS手势 UIGestureRecognizer实践(Swift)相关推荐

  1. iOS手势-UIGestureRecognizer

    原文链接 1. UIGestureRecognizer 父类是NSObject 利用手势识别器---UIGestureRecognizer,能轻松识别用户在某个view上面做的一些常见手势 UIGes ...

  2. 使用iOS手势UIGestureRecognizer

    UIKit中包含了UIGestureRecognizer类,用于检测发生在设备中的手势.UIGestureRecognizer是一个抽象类,定义了所有手势的基本行为,它有下面一些子类用于处理具体的手势 ...

  3. iOS 手势UIGestureRecognizer

    在 iPhone 或 iPad 的开发中,除了用 touchesBegan / touchesMoved / touchesEnded 这组方法来控制使用者的手指触控外,也可以用 UIGestureR ...

  4. IOS开发之手势——UIGestureRecognizer 共存

    IOS开发之手势--UIGestureRecognizer 共存 在 iPhone 或 iPad 的开发中,除了用 touchesBegan / touchesMoved / touchesEnded ...

  5. iOS手势学习UIGestureRecognizer cocos2d 手势推荐

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

  6. swift 实现iOS手势密码、指纹密码、faceID

    本博客包含了如何实现iOS手势密码.指纹密码.faceID全步骤,包括了完整的代码. 先附上demo地址https://github.com/Liuyubao/LYBTouchID,支持swift3. ...

  7. iOS手势操作简介(三)

    监听触摸事件的做法 如果想监听一个view上面的触摸事件,之前的做法是 自定义一个view 实现view的touches方法,在方法内部实现具体处理代码 通过touches方法监听view触摸事件,有 ...

  8. [iOS] 完整源码, Swift语言 - 账号保存工具

    代码地址如下: http://www.demodashi.com/demo/15017.html 1. 需求分析 作为一个开发者,平时肯定在各个平台,网站注册了各种账号:由于太多,很多时候都是注册之后 ...

  9. iOS 手势的用法

    iOS 手势类 : UIGestureRecognizer(继承于NSObject基类) 继承于UIGestureRecognizer类的有六个子类,分别是: UILongPressGestureRe ...

最新文章

  1. 年薪超 1400 万美元!苹果 CEO 库克去年薪酬大曝光!
  2. 如何打赢一场唯快不破的比赛,看看他们的绝招
  3. CentOS7中后台运行Node-RED(关闭窗口也能访问服务)
  4. 笔试题??智商题??(一)
  5. 单台web服务器性能,单台web服务器如何尽可能的提高网站性能
  6. MFC开发IM-第二篇、MFC picture 控件的用法
  7. 做好产品的孵化器 淘特上线10元店、淘特100
  8. hnu实验五 排队喝水
  9. 靠社交和游戏两张牌,腾讯还能活多久? | 畅言
  10. Unity3d基本优化条目
  11. 动态规划实战4-leetcode 5.Longest Palindromic Substring
  12. MATLAB显示图像变白问题
  13. 下行法求最小割集案例_最小割集求法.docx
  14. 桌面计算机图标变黑块,电脑中的文件夹图标变成黑色的方块的四种解决方法
  15. 将二进制转换成十进制 C语言
  16. 纵横算法之四:算法应该怎么学
  17. 爱奇艺笔试题之成长值计算
  18. 程序员进阶神器,ProcessOn绘制时序图
  19. There is a problem with this Windows Installer package
  20. python爬取历史天气查询_Python爬取南京历史天气数据(2345天气网)

热门文章

  1. VSCode导出jar(超简单)
  2. lvgl roller(滑动列表控件)
  3. Win32 API概述
  4. 国产数据库“向未来”
  5. 算法笔记——【分治法】线性时间选择
  6. 最全的Pandas 日期处理 超强总结!
  7. BCD码和十进制的转换
  8. 融资租赁业务系统整体介绍(一)
  9. java自带发送邮件,成都汇智动力-java邮件发送只需要java自带的mailjar
  10. iOS 第三方登录(QQ 微信 新浪微博)