本文介紹一些常使用的iOS手勢(shì)锤窑,基本上可以滿足項(xiàng)目需求坯辩。
單機(jī)手勢(shì)
let guesture = UITapGestureRecognizer(target: self, action: #selector(singleTap(_:)))
view.addGestureRecognizer(guesture)
func singleTap(_ tapGesture: UITapGestureRecognizer) {
}
雙擊手勢(shì)
let doubleGuesture = UITapGestureRecognizer(target: self, action: #selector(doubleTap(_:)))
doubleGuesture.numberOfTapsRequired = 2// 點(diǎn)擊次數(shù)
doubleGuesture.numberOfTouchesRequired = 1// 手指?jìng)€(gè)數(shù)
view.addGestureRecognizer(doubleGuesture)
func doubleTap(_ tapGesture: UITapGestureRecognizer) {
}
長(zhǎng)按手勢(shì)
let longGuesture = UILongPressGestureRecognizer(target: self, action: #selector(longPress(_:)))
view.addGestureRecognizer(longGuesture)
func longPress(_ longPressGesture: UILongPressGestureRecognizer){
}
滑動(dòng)手勢(shì)
left:向左滑動(dòng)速蕊,right:向右滑動(dòng)圆裕,up:向上滑動(dòng)际歼,down:向下滑動(dòng)说莫,
let leftSwipeGesture = UISwipeGestureRecognizer(target: self, action: #selector(swipeGesture(_:)))
leftSwipeGesture.direction= .left
view.addGestureRecognizer(leftSwipeGesture)
func swipeGesture(_ swipeGesture: UISwipeGestureRecognizer) {
switch swipeGesture.direction {
? case.left, .right, .up, .down:
? print("SwipeGesture")
}}
捏合手勢(shì)
let pinch = UIPinchGestureRecognizer(target: self, action: #selector(pinchGesture(_:)))
view.addGestureRecognizer(pinch)
func pinchGesture(_ pinchGesture: UIPinchGestureRecognizer) {
? print(pinchGesture.scale)// 捏合比例
? print(pinchGesture.velocity)// 捏合速度
}
旋轉(zhuǎn)手勢(shì)
let rotation = UIRotationGestureRecognizer(target: self, action: #selector(rotationGesture(_:)))
view.addGestureRecognizer(rotation)
func rotationGesture(_ rotationGesture: UIRotationGestureRecognizer) {
print(rotationGesture.rotation*(180/(CGFloat(Double.pi))))// 旋轉(zhuǎn)的角度
}
拖動(dòng)手勢(shì)
@IBOutlet weak var panGestureView: UIView!
let pan = UIPanGestureRecognizer(target: self, action: #selector(panGesture(_:)))
pan.maximumNumberOfTouches=1// 一個(gè)手指拖動(dòng)
panGestureView.addGestureRecognizer(pan)
func panGesture(_ panGesture: UIPanGestureRecognizer) {
let point = panGesture.location(in: panGesture.view?.superview)
panGestureView.center= point
}