我有一个自定义 UIGestureRecognizer,它可以正确识别预期的手势(两根手指的 Z 手势)并在 touchesEnded
中设置 state = .recognized
。问题是即使手势被识别,它有时会调用 Action 方法,有时不会。据我所知,这种情况不确定地发生。
有人知道为什么吗?
代码如下:
import UIKit
import UIKit.UIGestureRecognizerSubclass
class ZGestureRecognizer: UIGestureRecognizer {
private var topSwipeStartPoint = CGPoint.zero
private var diagonalSwipeStartPoint = CGPoint.zero
private var bottomSwipeStartPoint = CGPoint.zero
private let minDeltaX: CGFloat = 20
private let minDeltaY: CGFloat = 20
private var strokePhase = StrokePhase.notStarted
var trackedTouch: UITouch?
enum StrokePhase {
case notStarted
case topSwipe
case diagonalSwipe
case bottomSwipe
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent) {
if !(touches.count == 1 || touches.count == 2) {
state = .failed
return
}
if trackedTouch == nil {
trackedTouch = touches.min { $0.location(in: self.view?.window).x < $1.location(in: self.view?.window).x }
strokePhase = .topSwipe
topSwipeStartPoint = trackedTouch!.locationInWindow!
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent) {
guard let trackedTouch = trackedTouch,
trackedTouch.phase != .cancelled,
trackedTouch.phase != .ended
else {
self.state = .failed
return
}
let newPoint = trackedTouch.locationInWindow!
let previousPoint = trackedTouch.previousLocationInWindow!
switch strokePhase {
case .topSwipe:
if newPoint.x < previousPoint.x { // if we have started moving back to the left
let deltaX = previousPoint.x - topSwipeStartPoint.x
if deltaX > minDeltaX && touches.count == 2 {
diagonalSwipeStartPoint = previousPoint
strokePhase = .diagonalSwipe
}
else { // too short right swipe or not 2 touches
state = .failed
return
}
}
case .diagonalSwipe:
if newPoint.x > previousPoint.x { // if we have started moving back to the right
let deltaX = diagonalSwipeStartPoint.x - previousPoint.x
let deltaY = previousPoint.y - diagonalSwipeStartPoint.y
if deltaX > minDeltaX && deltaY > minDeltaY && touches.count == 2 {
bottomSwipeStartPoint = previousPoint
strokePhase = .bottomSwipe
}
else { // too short right swipe
state = .failed
return
}
}
case .bottomSwipe:
if newPoint.x < previousPoint.x || touches.count != 2 {
state = .failed
return
}
default: break
}
}
override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent) {
state = .failed
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent) {
guard strokePhase == .bottomSwipe else {
state = .failed
return
}
let endPoint = trackedTouch!.locationInWindow!
let bottomSwipeDeltaX = endPoint.x - bottomSwipeStartPoint.x
if bottomSwipeDeltaX > minDeltaX {
state = .recognized
}
else {
state = .failed
}
}
override func reset() {
strokePhase = .notStarted
trackedTouch = nil
}
}
由于手势识别在做适当的手势时总是以 state = .recognized
行结束,我认为代码不相关,但我很乐意出错。
我已经模拟了你的代码。我无法获得正确的手势(您没有提到所需的手势)。因此,在我的情况下, state = .recognized 从未被执行。然后,我在 touchesEnded 中硬编码 state = .recognized 以检查每次设置 state = .recognized 时是否调用 Action 方法。我发现它很好。
我发现所有重写的方法都缺少 super 调用。你可以把所有的 super 电话和检查一遍。另外,把登录触摸结束。我认为可能存在逻辑问题。因为,我不知道触发它所需的手势我无法调试代码。
关于ios - 自定义 UIGestureRecognizer 并不总是调用它的 action 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51222708/
欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://jike.in/) | Powered by Discuz! X3.4 |