ios - 自定义 UIGestureRecognizer 并不总是调用它的 action 方法
问题描述
我有一个自定义 UIGestureRecognizer 可以正确识别预期的手势(带有 2 个手指的 Z 手势)并设置state = .recognized
为touchesEnded
. 问题是即使手势被识别,它有时会调用动作方法,有时不会。据我所知,这种情况不确定地发生。
有谁知道为什么?
这是代码:
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 时是否调用动作方法。我发现它很好。
我发现所有被覆盖的方法都缺少 super call。你可以把所有的超级电话和检查一遍。另外,把登录触摸结束。我认为可能存在逻辑问题。因为,我不知道触发它所需的手势我无法调试代码。
推荐阅读
- python - ValueError: 层 max_pooling1d 的输入 0 与层不兼容:预期 ndim=3,发现 ndim=4。收到的完整形状:(无、128、1、32)
- ios - 尝试在 iOS 12 及更低版本中启动 Xamarin 应用程序时如何修复“错误 HE0042”
- javascript - Google Script - 添加编码以根据答案转到该部分的值
- android - RecyclerView Kotlin 不显示内容
- ios - 崩溃:com.apple.main-thread EXC_BAD_ACCESS 0x0000000282bed12e _CFHTTPServerResponseEnqueue
- laravel - BadMethodCallException:调用文件中未定义的方法 App\Order::meta()
- excel - 通过VBA的图形位置
- python - 计算百分比的加权平均值
- python - Plot horizontal line on a logit x-axis
- javascript - 如何对包含混合字符的字符串进行排序