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/2-100,y:65,width:100,height:100) self.view.addSubview(view1) view2.frame = CGRect(x:self.view.bounds.width/2-100,y:65+100+5,width:100,height:100) self.view.addSubview(view2) view3.frame = CGRect(x:self.view.bounds.width/2-100,y:65+100*2+5*2,width:100,height:100) self.view.addSubview(view3) view4.frame = CGRect(x:self.view.bounds.width/2-100,y:65+100*3+5*3,width:100,height:100) self.view.addSubview(view4) view5.frame = CGRect(x:self.view.bounds.width/2+50,y:65,width:100,height:100) self.view.addSubview(view5) view6.frame = CGRect(x:self.view.bounds.width/2+50,y:65+100+5,width:100,height:100) self.view.addSubview(view6) view7.frame = CGRect(x:self.view.bounds.width/2+50,y:65+100*2+5*2,width:100,height:100) self.view.addSubview(view7) view8.frame = CGRect(x:self.view.bounds.width/2+50,y:65+100*3+5*3,width:100,height:100) 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 = 1 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)") }
请发表评论