我想創建一個UIGestureRecognizer
子類,並在touchesMoved
我沒有代碼,但state
被設置爲.changed
。UIGestureRecognizer subclass-- touchesMoved自動更改狀態.changed
有誰知道爲什麼會發生這種情況?當我們處於.changed
狀態時,我需要能夠自己決定。
如果我刪除我的touchesBegan
功能並且從不移動到.began
,則不會發生這種情況。
import UIKit.UIGestureRecognizerSubclass
class MyRecognizer: UIGestureRecognizer {
private var refView: UIView? {
return self.view?.window
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent) {
super.touchesBegan(touches, with: event)
if touches.count != 1 {
state = .failed
return
}
guard let location = touches.first?.location(in: refView) else {
self.state = .failed
return
}
state = .began
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent) {
// there is no code here!
}
//This is how I'm debugging state
override var state: UIGestureRecognizerState {
get { return super.state }
set {
super.state = newValue
print("state = \(newValue.debugDescription)")
}
}
}
extension UIGestureRecognizerState: CustomDebugStringConvertible {
public var debugDescription: String {
switch self {
case .began: return "began"
case .possible: return "possible"
case .changed: return "changed"
case .ended: return "ended"
case .cancelled: return "cancelled"
case .failed: return "failed"
}
}
}
謝謝。不幸的是,我嘗試了沒有運氣。我從上面的例子中刪除了它,以顯示我甚至沒有觸及超類。我會重新加入,以便更清楚。 – MH175
看到我修改的答案。 – matt
謝謝。我現在看到 - 非常有幫助。除非觸摸移動了一定的最小數量,否則我試圖做的不是移動到.changed。 – MH175