Swift 多种手势
//轻拍
let tap = UITapGestureRecognizer()
self.view.addGestureRecognizer(tap)
tap.addTarget(self, action: #selector(tapAction))
//拍几下,
tap.numberOfTapsRequired = 2
//几个手指拍
tap.numberOfTouchesRequired = 2
//长按
let long = UILongPressGestureRecognizer()
self.view.addGestureRecognizer(long)
long.addTarget(self, action: #selector(longAction))
//长安的时间 默认是0.5
long.minimumPressDuration = 1
//长按时可以挪动的最小距离 默认是10
long.allowableMovement = 15
//长按的手指数,次数同轻拍
//轻扫
let swip = UISwipeGestureRecognizer()
swip.direction = .left
swip.addTarget(self, action: #selector(swipAction(swip:)))
self.view.addGestureRecognizer(swip)
let swip1 = UISwipeGestureRecognizer()
swip1.direction = .right
swip1.addTarget(self, action: #selector(swipAction(swip:)))
self.view.addGestureRecognizer(swip1)
//旋转
let rota = UIRotationGestureRecognizer()
self.view.addGestureRecognizer(rota)
rota.addTarget(self, action: #selector(rotaAction))
//旋转的角度 rota.rotation
//捏合
let pin = UIPinchGestureRecognizer()
pin.addTarget(self, action: #selector(pinAction))
self.view.addGestureRecognizer(pin)
//缩放比
//pin.scale
//拖动
let pan = UIPanGestureRecognizer()
pan.addTarget(self, action: #selector(panAction(pan:)))
self.view.addGestureRecognizer(pan)
//拖动的方法
func panAction (pan:UIPanGestureRecognizer) {
//返回当前拖动的位置在self.view上的位置
// print(pan.translation(in: self.view))
print(pan.translation(in: self.view))
}
//捏合的方法
func pinAction() {
print("捏合")
}
//旋转的方法
func rotaAction() {
print("旋转")
}
//轻扫的方法
func swipAction(swip:UISwipeGestureRecognizer) {
//获取轻扫的方向
let dir = swip.direction
if dir == .left {
}else if dir == .right {
}
}
//长按得方法
func longAction() {
print("长按")
}
//轻拍的方法
func tapAction() {
print("轻拍")
}