Swift - 各种手势检测大全(UIGestureRecognizer及其子类)
UIGestureRecognizer 有许多子类,用于监听一些常见的手势事件,这些子类主要有:
1、UISwipeGestureRecognizer:滑动(快速移动)
//滑动(快速移动)funcswipeGestureRecognizer(){letswipe=UISwipeGestureRecognizer(target:self,action:#selector(ViewController.swipe(_:)))swipe.direction=.up//监听方向self.view.addGestureRecognizer(swipe)}@objcfuncswipe(_recognizer:UISwipeGestureRecognizer){print("swipe ok")letpoint=recognizer.location(in:self.view)//这个点是滑动的起点print(point.x)print(point.y)}
注意1:如果 UISwipeGestureRecognizer 在不指定方向的时候,默认向右滑动才会触发事件。如果要指定方向,需要设置 direction 属性
swipe.direction=.up//监听方向
注意2:有网友问如果各个方向都要响应怎么办,只要多定义几个 UISwipeGestureRecognizer 就可以了
//滑动(快速移动)funcswipeGestureRecognizer(){letswipeUp=UISwipeGestureRecognizer(target:self,action:#selector(ViewController.swipe(_:)))swipeUp.direction=.upself.view.addGestureRecognizer(swipeUp)letswipeDown=UISwipeGestureRecognizer(target:self,action:#selector(ViewController.swipe(_:)))swipeDown.direction=.downself.view.addGestureRecognizer(swipeDown)}@objcfuncswipe(_recognizer:UISwipeGestureRecognizer){ifrecognizer.direction==.up{print("向上滑动")}elseifrecognizer.direction==.down{print("向下滑动")}letpoint=recognizer.location(in:self.view)//这个点是滑动的起点print(point.x)print(point.y)}
2、UIScreenEdgePanGestureRecognizer:边缘滑动
(1)这个是 UISwipeGestureRecognizer 的子类,与后者不同的时。UIScreenEdgePanGestureRecognizer 只触发从边缘开始的划动操作,比如从左侧边缘开始向右滑动。
(2)UIScreenEdgePanGestureRecognizer 没有 direction 属性,增加了个 edges 属性。表示要响应哪个边缘的划动操作。可以是从左边缘向右滑动,或右边缘向左、上边缘向下、下边缘向上、或响应全部边缘滑动。
// 边缘滑动funcscreenEdgePanGestureRecognizer(){letswipe=UIScreenEdgePanGestureRecognizer(target:self,action:#selector(screenEdgePanSwipe))swipe.edges=UIRectEdge.left//从左边缘开始滑动self.view.addGestureRecognizer(swipe)}@objcfuncscreenEdgePanSwipe(_recognizer:UIScreenEdgePanGestureRecognizer){print("left edgeswipe ok")letpoint=recognizer.location(in:self.view)//这个点是滑动的起点print(point.x)print(point.y)}
3、UITapGestureRecognizer:轻点手势(点击)
(1)可以通过 numberOfTouchesRequired 属性设置触摸点数,比如设置 2 表示必须两个手指触摸时才会触发
(2)通过 numberOfTapsRequired 属性设置点击次数,单击设置为 1,双击设置为 2
(3)如果一个控件既监听了单击事件也监听了双击事件,默认当双击事件触发的时候也同时会触发单击事件。如果想双击时不触发单击,需要通过 require(toFail:) 进行设置
// UITapGestureRecognizer:轻点手势(点击)functapGestureRecognizer(){//单击监听lettapSingle=UITapGestureRecognizer(target:self,action:#selector(tapSingleDid))tapSingle.numberOfTapsRequired=1tapSingle.numberOfTouchesRequired=1//双击监听lettapDouble=UITapGestureRecognizer(target:self,action:#selector(tapDoubleDid(_:)))tapDouble.numberOfTapsRequired=2tapDouble.numberOfTouchesRequired=1//声明点击事件需要双击事件检测失败后才会执行tapSingle.require(toFail:tapDouble)self.view.addGestureRecognizer(tapSingle)self.view.addGestureRecognizer(tapDouble)}@objcfunctapSingleDid(){print("单击了")}@objcfunctapDoubleDid(_sender:UITapGestureRecognizer){ifsender.view==self.view{print("双击了")}}``#####4、UIPinchGestureRecognizer:捏合手势(两个手指进行放大缩小)
// 捏合手势(两个手指进行放大缩小)
func pinchGestureRecognizer()
{
//设置监听方法为pinchDid方法
let pinch = UIPinchGestureRecognizer(target:self,action:#selector(pinchDid(_:)))
self.view.addGestureRecognizer(pinch)
}
@objc func pinchDid(_ recognizer:UIPinchGestureRecognizer) {
//在监听方法中可以实时获得捏合的比例
print(recognizer.scale)
//获取两个触摸点的坐标
print(recognizer.location(ofTouch: 0, in: self.view))
print(recognizer.location(ofTouch: 1, in: self.view))
}
``
注:recognizer.location使用这个的时候需要判断ofTouch: 1是否存在,因为当你手指捏合松开一个手指的瞬间这个是空的,会造成崩溃。
5、UIRotationGestureRecognizer:旋转手势(两个手指进行旋转)
// 旋转手势(两个手指进行旋转)funcrotationGestureRecognizer(){letrotation=UIRotationGestureRecognizer(target:self,action:#selector(rotationDid(_:)))self.view.addGestureRecognizer(rotation)}@objcfuncrotationDid(_recognizer:UIRotationGestureRecognizer){//旋转的弧度转换为角度print(recognizer.rotation*(180/CGFloat.pi))}
6、UIPanGestureRecognizer:拖动手势
// 拖动手势funcpanGestureRecognizer(){rect=UIView(frame:CGRect(x:0,y:0,width:100,height:100))rect.center=self.view.center rect.backgroundColor=UIColor.orangeself.view.addSubview(rect)letpan=UIPanGestureRecognizer(target:self,action:#selector(panDid(_:)))pan.maximumNumberOfTouches=1rect.addGestureRecognizer(pan)}@objcfuncpanDid(_recognizer:UISwipeGestureRecognizer){letpoint=recognizer.location(in:self.view)//设置矩形的位置rect.center=point}
通过 recognizer.view 我们可以直接得到触发事件的 view 对象,下面样例创建两个方块,但它们使用同一个拖动响应方法。(注意:由于一个 GestureRecognizer 只能对应一个 view,所以每个 view 还是要分别使用各自的 GestureRecognizer)
// 拖动手势funcpanGestureRecognizer2(){//定义两个方块letrect1=UIView(frame:CGRect(x:0,y:0,width:100,height:100))rect1.center=self.view.center rect1.backgroundColor=UIColor.orangeself.view.addSubview(rect1)letrect2=UIView(frame:CGRect(x:0,y:0,width:100,height:100))//rect2.center = self.view.centerrect2.backgroundColor=UIColor.yellowself.view.addSubview(rect2)//由于一个GestureRecognizer只能对应一个view,这里定义两个GestureRecognizerletpan1=UIPanGestureRecognizer(target:self,action:#selector(panDid2(_:)))pan1.maximumNumberOfTouches=1rect1.addGestureRecognizer(pan1)letpan2=UIPanGestureRecognizer(target:self,action:#selector(panDid2(_:)))pan2.maximumNumberOfTouches=1rect2.addGestureRecognizer(pan2)}//两个方块都使用同一拖拽响应方法@objcfuncpanDid2(_recognizer:UIPanGestureRecognizer){letpoint=recognizer.location(in:self.view)//设置矩形的位置recognizer.view?.center=point}
7、UILongPressGestureRecognizer:长按
/// 长按funclongPressGestureRecognizer(){//长按监听letlongPress=UILongPressGestureRecognizer(target:self,action:#selector(longPressDid(_:)))self.view.addGestureRecognizer(longPress)}@objcfunclongPressDid(_sender:UILongPressGestureRecognizer){ifsender.state==.began{print("长按响应开始")}else{print("长按响应结束")}}
网友评论