05.手势的处理逻辑
目录介绍
- 01.手势类型介绍
- 1.1 手势父类介绍
- 1.2 手势类有哪些
- 02.手势处理使用
- 2.1 手势处理步骤
- 2.2 各种手势用法
- 2.3 触控事件理解
01.手势类型介绍
1.1 手势父类介绍
在 iOS 中,UIGestureRecognizer 是一个用于处理用户手势的抽象基类。
它提供了一种简单的方式来检测和响应用户的触摸手势,例如点击、滑动、捏合、旋转等。
1.2 手势类有哪些
手势类型(他们都继承自UIGestureRecognizer,而UIGestureRecognizer继承自NSObject)
UIPanGestureRecognizer(拖动)
UIPinchGestureRecognizer(捏合)
UIRotationGestureRecognizer(旋转)
UITapGestureRecognizer(点按)
UILongPressGestureRecognizer(长按)
UISwipeGestureRecognizer(轻扫)
02.手势处理使用
2.1 手势处理步骤
1.创建手势识别器:创建了一个 UITapGestureRecognizer 实例,并指定了一个目标对象和一个处理方法。
let gestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleTap(_:)))
2.添加手势识别器到视图:将手势识别器添加到视图中,以便它可以接收用户的手势事件。
view.addGestureRecognizer(gestureRecognizer)
view.removeGestureRecognizer(gestureRecognizer) //移除手势
3.实现处理方法:实现了一个名为 handleTap(_😃 的处理方法,用于处理手势事件。
请注意,处理方法需要使用 @objc 标记,并且参数类型必须与手势识别器的类型匹配。
@objc func handleTap(_ gestureRecognizer: UITapGestureRecognizer) {
// 处理手势事件
}
4.可选:配置手势识别器的属性:设置了手势识别器的 numberOfTapsRequired 属性为 2,表示需要双击才能触发手势事件。我们还将 isEnabled 属性设置为 true,以启用手势识别器。
gestureRecognizer.numberOfTapsRequired = 2
gestureRecognizer.isEnabled = true
2.2 各种手势用法
@IBOutlet weak var imgView: UIImageView!
var lastScaleFactor: CGFloat = 1
var netRotation: CGFloat = 0
var netTranslation: CGPoint = CGPointMake(0,0)
override func viewDidLoad() {
super.viewDidLoad()
// 点击手势
let tapGesture = UITapGestureRecognizer(target: self, action: "handleTapGesture:")
tapGesture.numberOfTapsRequired = 2 // 点击次数
self.view.addGestureRecognizer(tapGesture)
// 捏手势
let pinchGesture = UIPinchGestureRecognizer(target: self, action: "handlePinchGesture:")
self.view.addGestureRecognizer(pinchGesture)
// 旋转手势
let rotateGesture = UIRotationGestureRecognizer(target: self, action: "handleRotateGesture:")
self.view.addGestureRecognizer(rotateGesture)
// 滑动手势: 默认为右滑
let swipeGesture = UISwipeGestureRecognizer(target: self, action: "handleSwipeGesture:")
swipeGesture.direction = .Down //.Left .Down .Up .Right
self.view.addGestureRecognizer(swipeGesture)
// // 拖动手势: 与滑动手势冲突,二者选其一
// let panGesture = UIPanGestureRecognizer(target: self, action: "handlePanGesture:")
// self.view.addGestureRecognizer(panGesture)
// 长按手势
let longpressGestrue = UILongPressGestureRecognizer(target: self, action: "handleLongPressGesture:")
longpressGestrue.minimumPressDuration = 1
longpressGestrue.numberOfTouchesRequired = 1
longpressGestrue.allowableMovement = 15
self.view.addGestureRecognizer(longpressGestrue)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
// MARK: gesture methods
func handleTapGesture(sender: UITapGestureRecognizer) {
print("handleTapGesture")
if imgView.contentMode == UIViewContentMode.ScaleAspectFit {
imgView.contentMode = UIViewContentMode.Center
} else {
imgView.contentMode = UIViewContentMode.ScaleAspectFit
}
}
func handlePinchGesture(sender: UIPinchGestureRecognizer) {
print("handlePinchGesture")
let factor = sender.scale
print("factor", factor)
if factor > 1 { // 放大
imgView.transform = CGAffineTransformMakeScale(lastScaleFactor + factor - 1, lastScaleFactor + factor - 1)
} else { // 缩小
imgView.transform = CGAffineTransformMakeScale(lastScaleFactor * factor, lastScaleFactor * factor)
}
if sender.state == UIGestureRecognizerState.Ended {
if factor > 1 {
lastScaleFactor = lastScaleFactor + factor - 1
} else {
lastScaleFactor = lastScaleFactor * factor
}
}
}
func handleRotateGesture(sender: UIRotationGestureRecognizer) {
print("handleRotateGesture")
let rotation = sender.rotation
print("rotation", rotation)
imgView.transform = CGAffineTransformMakeRotation(rotation + netRotation)
if sender.state == UIGestureRecognizerState.Ended {
netRotation += rotation
}
}
func handleSwipeGesture(sender: UISwipeGestureRecognizer) {
print("handleSwipeGesture")
}
func handlePanGesture(sender: UIPanGestureRecognizer) {
print("UIPanGestureRecognizer")
let translation = sender.translationInView(imgView)
imgView.transform = CGAffineTransformMakeTranslation(netTranslation.x + translation.x, translation.y + netTranslation.y)
if sender.state == UIGestureRecognizerState.Ended {
netTranslation.x += translation.x
netTranslation.y += translation.y
}
}
func handleLongPressGesture(sender: UILongPressGestureRecognizer) {
print("handleLongPressGesture")
if sender.state == UIGestureRecognizerState.Began {
print("----------")
}
}
2.3 触控事件理解
- func touchesBegan(touches: NSSet, withEvent event: UIEvent) 通知调用者当有一个或者多个手指触摸到了视图或者窗口时触发此方法
- func touchesMoved(touches: NSSet, withEvent event: UIEvent) 告诉接收者一个或者多个手指在视图或者窗口上触发移动事件
- func touchesEnded(touches: NSSet, withEvent event: UIEvent) 当一个触摸事件结束时发出的UITouch实例对象
- func touchesCancelled(touches: NSSet, withEvent event: UIEvent) 通知接收者当系统发出取消事件的时候(如低内存消耗的告警框)
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch: AnyObject in touches {
let tap: UITouch = touch as! UITouch
print("tapCount", tap.tapCount)
print("event begin!")
}
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch: AnyObject in touches {
let tap: UITouch = touch as! UITouch
print(tap.locationInView(self.view))
}
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
print("event end!")
}
override func touchesCancelled(touches: Set<UITouch>?, withEvent event: UIEvent?) {
print("event canceled!")
}