iOS手势处理由UIGestureRecognizer
控制,不同手势有这个类的不同子类处理,不同手势处理有不同的相关属性。
子类 |
描述 |
UITapGestureRecognizer |
点击手势 |
UIPanGestureRecognizer |
跟着手移动手势 |
UIPinchGestureRecognizer |
缩放手势 |
UIRotationGestureRecognizer |
旋转手势 |
UISwipeGestureRecognizer |
轻扫手势 |
UILongPressGestureRecognizer |
长按手势 |
UIScreenEdgePanGestureRecognizer |
屏幕边缘滑动手势 |
手势处理的步骤一般:
- 初始化手势,并添加到需要手势的View中
- 添加手势回调方法
UITapGestureRecognizer
属性numberOfTapsRequired
表示点击次数,属性numberOfTouchesRequired
表示点击手指数。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
var isChange = false
...
let tap = UITapGestureRecognizer(target: self, action: #selector(ViewController.tap(tap:)))
tap.numberOfTapsRequired = 2
...
imageView.addGestureRecognizer(tap)
...
@objc func tap(tap: UITapGestureRecognizer) {
let center = imageView.center
if isChange {
imageView.frame.size.width /= 2
imageView.frame.size.height /= 2
imageView.center = center
isChange = false
} else {
imageView.frame.size.width *= 2
imageView.frame.size.height *= 2
imageView.center = center
isChange = true
}
}
|
UIPanGestureRecognizer
1
|
let pan = UIPanGestureRecognizer(target: self, action: #selector(ViewController.pan(pan:)))
|
1
2
3
4
5
6
7
8
9
10
|
@objc func pan(pan: UIPanGestureRecognizer) {
if pan.state == .began || pan.state == .changed {
// 移动后的变化(变化值类似向量)
let translation = pan.translation(in: imageView.superview)
print(translation)
imageView.center = CGPoint(x: imageView.center.x + translation.x, y: imageView.center.y + translation.y)
// view移动后,把上一步的移动值变为0,否则移动值为递增
pan.setTranslation(CGPoint.zero, in: imageView.superview)
}
}
|
UIRotationGestureRecognizer
1
|
let rotation = UIRotationGestureRecognizer(target: self, action: #selector(ViewController.rotation(rotation:)))
|
1
2
3
4
5
|
@objc func rotation(rotation: UIRotationGestureRecognizer) {
if rotation.state == .began || rotation.state == .changed {
imageView.transform = CGAffineTransform(rotationAngle: rotation.rotation)
}
}
|
UISwipeGestureRecognizer
1
2
3
|
let swipe = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.swipe(swipe:)))
swipe.direction = .up
swipe.numberOfTouchesRequired = 3
|
1
2
3
|
@objc func swipe(swipe: UISwipeGestureRecognizer) {
print("扫的方向:\(swipe.direction),扫的手指数:\(swipe.numberOfTouchesRequired)")
}
|
UILongPressGestureRecognizer
numberOfTouchesRequired
长按的指头数
minimumPressDuratio
长按最小时间(秒)
1
2
3
4
5
6
7
8
9
|
let long = UILongPressGestureRecognizer(target: self, action: #selector(ViewController.long(long:)))
long.numberOfTouchesRequired = 2
long.minimumPressDuration = 1
...
@objc func long(long: UILongPressGestureRecognizer) {
print("长按手势,长按字头数为\(long.numberOfTouchesRequired)")
}
|
UIScreenEdgePanGestureRecognizer
1
2
|
let screenEdge = UIScreenEdgePanGestureRecognizer(target: self, action: #selector(ViewController.screenEdage(screenEdage:)))
screenEdge.edges = .left
|
1
2
3
|
@objc func screenEdage(screenEdage: UIScreenEdgePanGestureRecognizer) {
print("屏幕边缘滑动手势:\(screenEdage.edges)")
}
|
详细代码: GestureDemo
文章作者
andyron
上次更新
2024-07-16
(41dd7f9)
文章
许可协议
原创文章,如需转载请注明文章作者和出处。谢谢!