【发布时间】:2020-09-28 05:24:23
【问题描述】:
我在视图中添加了捏合手势和平移手势。我也在使用 touchesBegan 和 touchesMoved 方法。令人惊讶的是,当我捏住我的视图时,它有时也会调用 touchesBegan 和 touchesMoved。如何在捏住时停止调用此方法?这就是我所做的。
class CustomView: UIView {
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesBegan(touches, with: event)
if let touchCount = event?.touches(for: self)?.count {
if touchCount == 1 {
// Doing some task
}
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesMoved(touches, with: event)
if let touchCount = event?.touches(for: self)?.count {
if touchCount == 1 {
// Doing some task
}
}
}
}
在我的 ViewController 中:我创建了一个 CustomView 类的 customView 对象并添加了一些手势,如下所示:
let pinch = UIPinchGestureRecognizer(target: self, action: #selector(pinch(recognizer:)))
customView.addGestureRecognizer(pinch)
let panGesture = UIPanGestureRecognizer(target: self, action: #selector(handlePan(recognizer:)))
panGesture.minimumNumberOfTouches = 2
self.customView.addGestureRecognizer(panGesture)
【问题讨论】: