本文介绍一些常使用的iOS手势,基本上可以满足项目需求。
手势.gif单机手势
let guesture = UITapGestureRecognizer(target: self, action: #selector(singleTap(_:)))
view.addGestureRecognizer(guesture)
func singleTap(_ tapGesture: UITapGestureRecognizer) {
}
双击手势
let doubleGuesture = UITapGestureRecognizer(target: self, action: #selector(doubleTap(_:)))
doubleGuesture.numberOfTapsRequired = 2// 点击次数
doubleGuesture.numberOfTouchesRequired = 1// 手指个数
view.addGestureRecognizer(doubleGuesture)
func doubleTap(_ tapGesture: UITapGestureRecognizer) {
}
长按手势
let longGuesture = UILongPressGestureRecognizer(target: self, action: #selector(longPress(_:)))
view.addGestureRecognizer(longGuesture)
func longPress(_ longPressGesture: UILongPressGestureRecognizer){
}
滑动手势
left:向左滑动,right:向右滑动,up:向上滑动,down:向下滑动,
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")
}}
捏合手势
let pinch = UIPinchGestureRecognizer(target: self, action: #selector(pinchGesture(_:)))
view.addGestureRecognizer(pinch)
func pinchGesture(_ pinchGesture: UIPinchGestureRecognizer) {
print(pinchGesture.scale)// 捏合比例
print(pinchGesture.velocity)// 捏合速度
}
旋转手势
let rotation = UIRotationGestureRecognizer(target: self, action: #selector(rotationGesture(_:)))
view.addGestureRecognizer(rotation)
func rotationGesture(_ rotationGesture: UIRotationGestureRecognizer) {
print(rotationGesture.rotation*(180/(CGFloat(Double.pi))))// 旋转的角度
}
拖动手势
@IBOutlet weak var panGestureView: UIView!
let pan = UIPanGestureRecognizer(target: self, action: #selector(panGesture(_:)))
pan.maximumNumberOfTouches=1// 一个手指拖动
panGestureView.addGestureRecognizer(pan)
func panGesture(_ panGesture: UIPanGestureRecognizer) {
let point = panGesture.location(in: panGesture.view?.superview)
panGestureView.center= point
}
网友评论