UIGestureRecognizer 有许多子类,用于监听一些常见的手势事件,这些子类主要有:
原文出自: 转载请保留原文链接:
1,UISwipeGestureRecognizer:滑动(快速移动)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | import UIKit class ViewController : UIViewController { override func viewDidLoad() { super .viewDidLoad() let swipe = UISwipeGestureRecognizer (target: self , action:#selector(swipe(_:))) swipe.direction = .up self .view.addGestureRecognizer(swipe) } func swipe(_ recognizer: UISwipeGestureRecognizer ){ print ( "swipe ok" ) let point=recognizer.location( in : self .view) //这个点是滑动的起点 print (point.x) print (point.y) } } |
注意1:如果 UISwipeGestureRecognizer 在不指定方向的时候,默认向右滑动才会触发事件。如果要指定方向,需要设置 direction 属性
1 2 | //表示监听滑动的方向为向上 swipe.direction = .up |
注意2:有网友问如果各个方向都要响应怎么办,只要多定义几个 UISwipeGestureRecognizer 就可以了
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | import UIKit class ViewController : UIViewController { override func viewDidLoad() { super .viewDidLoad() let swipeUp = UISwipeGestureRecognizer (target: self , action:#selector(swipe(_:))) swipeUp.direction = .up self .view.addGestureRecognizer(swipeUp) let swipeDown = UISwipeGestureRecognizer (target: self , action:#selector(swipe(_:))) swipeDown.direction = .down self .view.addGestureRecognizer(swipeDown) } func swipe(_ recognizer: UISwipeGestureRecognizer ){ if recognizer.direction == .up{ print ( "向上滑动" ) } else if recognizer.direction == .down{ print ( "向下滑动" ) } let point=recognizer.location( in : self .view) //这个点是滑动的起点 print (point.x) print (point.y) } } |
2,UIScreenEdgePanGestureRecognizer:边缘滑动
(1)这个是 UISwipeGestureRecognizer 的子类,与后者不同的时。UIScreenEdgePanGestureRecognizer 只触发从边缘开始的划动操作,比如从左侧边缘开始向右滑动。 (2)UIScreenEdgePanGestureRecognizer 没有 direction 属性,增加了个 edges 属性。表示要响应哪个边缘的划动操作。可以是从左边缘向右滑动,或右边缘向左、上边缘向下、下边缘向上、或响应全部边缘滑动。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | import UIKit class ViewController : UIViewController { override func viewDidLoad() { super .viewDidLoad() let swipe = UIScreenEdgePanGestureRecognizer (target: self , action:#selector(swipe(_:))) swipe.edges = UIRectEdge .left //从左边缘开始滑动 self .view.addGestureRecognizer(swipe) } func swipe(_ recognizer: UIScreenEdgePanGestureRecognizer ){ print ( "left edgeswipe ok" ) let point=recognizer.location( in : self .view) //这个点是滑动的起点 print (point.x) print (point.y) } } |
3,UITapGestureRecognizer:轻点手势(点击)
(1)可以通过 numberOfTouchesRequired 属性设置触摸点数,比如设置 2 表示必须两个手指触摸时才会触发 (2)通过 numberOfTapsRequired 属性设置点击次数,单击设置为 1,双击设置为 2 (3)如果一个控件既监听了单击事件也监听了双击事件,默认当双击事件触发的时候也同时会触发单击事件。如果想双击时不触发单击,需要通过 require(toFail:) 进行设置 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | import UIKit class ViewController : UIViewController { override func viewDidLoad() { super .viewDidLoad() //单击监听 let tapSingle= UITapGestureRecognizer (target: self ,action:#selector(tapSingleDid)) tapSingle.numberOfTapsRequired = 1 tapSingle.numberOfTouchesRequired = 1 //双击监听 let tapDouble= UITapGestureRecognizer (target: self ,action:#selector(tapDoubleDid(_:))) tapDouble.numberOfTapsRequired = 2 tapDouble.numberOfTouchesRequired = 1 //声明点击事件需要双击事件检测失败后才会执行 tapSingle.require(toFail: tapDouble) self .view.addGestureRecognizer(tapSingle) self .view.addGestureRecognizer(tapDouble) } func tapSingleDid(){ print ( "单击了" ) } func tapDoubleDid(_ sender: UITapGestureRecognizer ){ if sender.view == self .view{ print ( "双击了" ) } } } |
4,UIPinchGestureRecognizer:捏合手势(两个手指进行放大缩小)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | import UIKit class ViewController : UIViewController { override func viewDidLoad() { super .viewDidLoad() //设置监听方法为pinchDid方法 let pinch = UIPinchGestureRecognizer (target: self ,action:#selector(pinchDid(_:))) self .view.addGestureRecognizer(pinch) } func pinchDid(_ recognizer: UIPinchGestureRecognizer ) { //在监听方法中可以实时获得捏合的比例 print (recognizer.scale); } } |
5,UIRotationGestureRecognizer:旋转手势(两个手指进行旋转)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | import UIKit class ViewController : UIViewController { override func viewDidLoad() { super .viewDidLoad() let rotation = UIRotationGestureRecognizer (target: self , action:#selector(rotationDid(_:))) self .view.addGestureRecognizer(rotation) } func rotationDid(_ recognizer: UIRotationGestureRecognizer ){ //旋转的弧度转换为角度 print (recognizer.rotation*(180/ CGFloat ( M_PI ))) } } |
6,UIPanGestureRecognizer:拖动手势
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | import UIKit class ViewController : UIViewController { var rect: UIView ! override func viewDidLoad() { super .viewDidLoad() rect = UIView (frame: CGRect (x:0, y:0, width:100, height:100)) rect.center = self .view.center rect.backgroundColor = UIColor .orange self .view.addSubview(rect) let pan = UIPanGestureRecognizer (target: self ,action:#selector(panDid(_:))) pan.maximumNumberOfTouches = 1 rect.addGestureRecognizer(pan) } func panDid(_ recognizer: UISwipeGestureRecognizer ){ let point=recognizer.location( in : self .view) //设置矩形的位置 rect.center=point } } |
通过 recognizer.view 我们可以直接得到触发事件的 view 对象,下面样例创建两个方块,但它们使用同一个拖动响应方法。
(注意:由于一个 GestureRecognizer 只能对应一个 view,所以每个 view 还是要分别使用各自的 GestureRecognizer)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | import UIKit class ViewController : UIViewController { override func viewDidLoad() { super .viewDidLoad() //定义两个方块 let rect1 = UIView (frame: CGRect (x:0, y:0, width:100, height:100)) rect1.center = self .view.center rect1.backgroundColor = UIColor .orange self .view.addSubview(rect1) let rect2 = UIView (frame: CGRect (x:0, y:0, width:100, height:100)) //rect2.center = self.view.center rect2.backgroundColor = UIColor .yellow self .view.addSubview(rect2) //由于一个GestureRecognizer只能对应一个view,这里定义两个GestureRecognizer let pan1 = UIPanGestureRecognizer (target: self ,action:#selector(panDid(_:))) pan1.maximumNumberOfTouches = 1 rect1.addGestureRecognizer(pan1) let pan2 = UIPanGestureRecognizer (target: self ,action:#selector(panDid(_:))) pan2.maximumNumberOfTouches = 1 rect2.addGestureRecognizer(pan2) } //两个方块都使用同一拖拽响应方法 func panDid(_ recognizer: UIPanGestureRecognizer ){ let point=recognizer.location( in : self .view) //设置矩形的位置 recognizer.view?.center = point } } |
7,UILongPressGestureRecognizer:长按
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | import UIKit class ViewController : UIViewController { override func viewDidLoad() { super .viewDidLoad() //长按监听 let longPress = UILongPressGestureRecognizer (target: self , action:#selector(longPressDid(_:))) self .view.addGestureRecognizer(longPress) } func longPressDid(_ sender: UILongPressGestureRecognizer ){ if sender.state == .began { print ( "长按响应开始" ) } else { print ( "长按响应结束" ) } } } |