美文网首页
Swift - 手势UIGestureRecognizer及其子

Swift - 手势UIGestureRecognizer及其子

作者: 解码漫聊 | 来源:发表于2018-01-19 16:51 被阅读0次

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

    1.UISwipeGestureRecognizer:滑动(快速移动)

    import UIKit
    
    class ViewController: UIViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
            //滑动手势
            let swipe = UISwipeGestureRecognizer(target: self, action: #selector(swipe(recognizer:)))
            swipe.direction = .up
            self.view.addGestureRecognizer(swipe)
            
        }
    
        @objc func swipe(recognizer:UISwipeGestureRecognizer) {
            print("swipe swipe")
            let point = recognizer.location(in: view)
            print("滑动起点坐标(x:\(point.x), y:\(point.y))")
        }
    }
    

    打印结果:

    swipe swipe
    滑动起点坐标(x:196.0, y:336.666656494141)
    

    注意1:如果 UISwipeGestureRecognizer 在不指定方向的时候,默认向右滑动才会触发事件。如果要指定方向,需要设置 direction 属性:

    //表示监听滑动的方向为向上
    swipe.direction = .up
    

    如果希望各个方向都响应手势,定义不同方向的UISwipeGestureRecognizer,添加到被监听对象:

    import UIKit
    
    class ViewController: UIViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
            //向上滑动手势
            let swipeUp = UISwipeGestureRecognizer(target: self, action: #selector(swipe(recognizer:)))
            swipeUp.direction = .up
            self.view.addGestureRecognizer(swipeUp)
            
            //向下滑动手势
            let swipeDown = UISwipeGestureRecognizer(target: self, action: #selector(swipe(recognizer:)))
            swipeDown.direction = .down
            view.addGestureRecognizer(swipeDown)
        }
    
        @objc func swipe(recognizer:UISwipeGestureRecognizer) {
            if recognizer.direction == .up {
                print("这是向上滑动")
            } else if recognizer.direction == .down {
                print("这是向下滑动")
            }
            let point = recognizer.location(in: view)
            print("滑动起点坐标(x:\(point.x), y:\(point.y))")
        }
    }
    

    打印结果:

    这是向下滑动
    滑动起点坐标(x:192.0, y:271.666656494141)
    这是向上滑动
    滑动起点坐标(x:212.33332824707, y:454.33332824707)
    

    2.UIScreenEdgePanGestureRecognizer:边缘滑动

    • 这个是 UISwipeGestureRecognizer 的子类,与后者不同的时。UIScreenEdgePanGestureRecognizer 只触发从边缘开始的划动操作,比如从左侧边缘开始向右滑动。

    • UIScreenEdgePanGestureRecognizer 没有 direction 属性,增加了个 edges 属性。表示要响应哪个边缘的划动操作。可以是从左边缘向右滑动,或右边缘向左、上边缘向下、下边缘向上、或响应全部边缘滑动。

    import UIKit
    
    class ViewController: UIViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
            let edgeSwipe = UIScreenEdgePanGestureRecognizer(target: self, action: #selector(edgeSwipe(recognizer:)))
            //从右边缘开始滑动
            edgeSwipe.edges = .right
            view.addGestureRecognizer(edgeSwipe)
            
        }
    
        @objc func edgeSwipe(recognizer:UIScreenEdgePanGestureRecognizer) {
            if recognizer.edges == .right {
                print("这是从右边开始滑动")
            }
            let point = recognizer.location(in: view)
            print("滑动起点坐标(x:\(point.x), y:\(point.y))")
        }
    }
    

    3.UITapGestureRecognizer:轻点手势(点击)

    • 可以通过 numberOfTouchesRequired 属性设置触摸点数,比如设置 2 表示必须两个手指触摸时才会触发
    • 通过 numberOfTapsRequired 属性设置点击次数,单击设置为 1,双击设置为 2
    • 如果一个控件既监听了单击事件也监听了双击事件,默认当双击事件触发的时候也同时会触发单击事件。如果想双击时不触发单击,需要通过 require(toFail:) 进行设置
    import UIKit
    
    class ViewController: UIViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
        
            //单击
            let singleTap = UITapGestureRecognizer(target: self, action: #selector(singleTap(recognizer:)))
            singleTap.numberOfTapsRequired = 1
            singleTap.numberOfTouchesRequired = 1
            
            //双击
            let doubleTap = UITapGestureRecognizer(target: self, action: #selector(doubleTap(recognizer:)))
            doubleTap.numberOfTapsRequired = 2
            doubleTap.numberOfTouchesRequired = 1
            
            //声明单击事件需要双击事件检测失败后才会执行
            singleTap.require(toFail: doubleTap)
            
            view.addGestureRecognizer(singleTap)
            view.addGestureRecognizer(doubleTap)
            
        }
    
        @objc func singleTap(recognizer:UITapGestureRecognizer) {
            print("这是单击")
        }
        
        @objc func doubleTap(recognizer:UITapGestureRecognizer) {
            print("这是双击")
        }
    }
    

    4.UIPinchGestureRecognizer:捏合手势(两个手指进行放大缩小)

    class ViewController: UIViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
        
            let pinch = UIPinchGestureRecognizer(target: self, action: #selector(pinch(recognizer:)))
            
            view.addGestureRecognizer(pinch)
        }
    
        @objc func pinch(recognizer:UIPinchGestureRecognizer) {
            //实时获取捏合的比例
            print("捏合比例:\(recognizer.scale)")
            
            //获取两个触摸点的坐标
            print(recognizer.location(ofTouch: 0, in: view))
            print(recognizer.location(ofTouch: 1, in: view))
        }
    }
    

    注意:模拟器测试捏合和旋转手势时,按住 option 键,再用触摸板或鼠标操作

    5.UIRotationGestureRecognizer:旋转手势(两个手指进行旋转)

    import UIKit
    
    class ViewController: UIViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
            
            let rotation = UIRotationGestureRecognizer(target: self, action:
                #selector(rotation(recognizer:)))
            
            view.addGestureRecognizer(rotation)
        }
    
        @objc func rotation(recognizer:UIRotationGestureRecognizer) {
            //旋转的弧度转换为角度
            print(recognizer.rotation*(180/CGFloat.pi))
    
        }
    }
    

    6.UIPanGestureRecognizer:拖动手势

    import UIKit
    
    class ViewController: UIViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
            
            let redView = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
            redView.backgroundColor = UIColor.red
            redView.center = view.center
            view.addSubview(redView)
            
            let pan = UIPanGestureRecognizer(target: self, action:
                #selector(pan(recognizer:)))
            
            redView.addGestureRecognizer(pan)
        }
    
        @objc func pan(recognizer:UIRotationGestureRecognizer) {
            
            let point = recognizer.location(in: view)
            //设置矩形的位置
            recognizer.view?.center = point
            
        }
    }
    

    注意

    • 通过 recognizer.view 我们可以直接得到触发事件的 view 对象
    • 一个 GestureRecognizer 只能对应一个 view,所以每个 view 还是要分别使用各自的 GestureRecognizer

    实例:

    import UIKit
    
    class ViewController: UIViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
            
            let redView = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
            redView.backgroundColor = UIColor.red
            redView.center = view.center
            view.addSubview(redView)
            
            let yellowView = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
            yellowView.backgroundColor = UIColor.yellow
            view.addSubview(yellowView)
            
            
            let pan1 = UIPanGestureRecognizer(target: self, action:
                #selector(pan(recognizer:)))
            redView.addGestureRecognizer(pan1)
            
            let pan2 = UIPanGestureRecognizer(target: self, action:
                #selector(pan(recognizer:)))
            yellowView.addGestureRecognizer(pan2)
        }
    
        @objc func pan(recognizer:UIRotationGestureRecognizer) {
            
            let point = recognizer.location(in: view)
            //设置矩形的位置
            recognizer.view?.center = point
            
        }
    }
    

    7.UILongPressGestureRecognizer:长按

    import UIKit
    
    class ViewController: UIViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
            
            let longPress = UILongPressGestureRecognizer(target: self, action:
                #selector(longPress(recognizer:)))
            view.addGestureRecognizer(longPress)
        }
    
        @objc func longPress(recognizer:UIRotationGestureRecognizer) {
            if recognizer.state == .began {
                print("长按响应开始")
            } else {
                print("长按响应结束")
            }
        }
    }
    

    相关文章

      网友评论

          本文标题:Swift - 手势UIGestureRecognizer及其子

          本文链接:https://www.haomeiwen.com/subject/ougooxtx.html