Fork me on GitHub

iOS tutorial 13:手势处理

iOS手势处理由UIGestureRecognizer控制,不同手势有这个类的不同子类处理,不同手势处理有不同的相关属性。

子类 描述
UITapGestureRecognizer 点击手势
UIPanGestureRecognizer 跟着手移动手势
UIPinchGestureRecognizer 缩放手势
UIRotationGestureRecognizer 旋转手势
UISwipeGestureRecognizer 轻扫手势
UILongPressGestureRecognizer 长按手势
UIScreenEdgePanGestureRecognizer 屏幕边缘滑动手势

手势处理的步骤一般:

  1. 初始化手势,并添加到需要手势的View中
  2. 添加手势回调方法

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


--------------------本文结束👨‍💻感谢您的阅读--------------------
坚持原创技术分享,您的支持将鼓励我继续创作!
  • 本文标题: iOS tutorial 13:手势处理
  • 本文作者: AndyRon
  • 发布时间: 2017年08月22日 - 00:00
  • 最后更新: 2019年09月03日 - 13:46
  • 本文链接: http://andyron.com/2017/ios-tutorial-13.html
  • 版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 3.0 许可协议。转载请注明出处!
0%