gpt4 book ai didi

ios - UIGestureRecognizer 子类——touchesMoved 自动改变状态为.changed

转载 作者:行者123 更新时间:2023-11-28 12:18:56 24 4
gpt4 key购买 nike

我正在尝试创建一个 UIGestureRecognizer 子类,在 touchesMoved 中我没有代码,但是 state 被设置为 .改变

有人知道为什么会这样吗?当我们处于 .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"
}
}
}

最佳答案

您的测试方法有缺陷。我试过了(这是我的测试应用程序的完整代码):

import UIKit
import UIKit.UIGestureRecognizerSubclass

class MyRecognizer: UIGestureRecognizer {
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent) {
super.touchesBegan(touches, with: event)
print(self.state.rawValue)
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent) {
super.touchesMoved(touches, with: event)
print(self.state.rawValue)
}
}

class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
let g = MyRecognizer(target: self, action: #selector(grFired))
self.view.addGestureRecognizer(g)
// Do any additional setup after loading the view, typically from a nib.
}

@objc func grFired(_ g:UIGestureRecognizer) {
print("here", g.state.rawValue)
}

}

然后我拖到背景 View 上,手势识别器附加到该 View 上。我的手势识别器从未从 possible 移动过状态。

另请注意,您的期望可能不正确(“当我们处于 .changed 状态时,我需要能够自己决定”)。正常和正确的行为是,声明状态为 .begantouchesBegan , 你的 touchesMoved将使用状态 .began 调用一次并且手势识别器将立即前进到 .changed自动地。这是正确的;如果这是连续手势,则不可能是 moved当我们在 .began 时,事件应该进来没有我们继续.changed的状态.同样,这是一个测试:

import UIKit
import UIKit.UIGestureRecognizerSubclass

class MyRecognizer: UIGestureRecognizer {
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent) {
super.touchesBegan(touches, with: event)
if touches.count != 1 {
state = .failed
return
}
print(self.state.rawValue)
self.state = .began
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent) {
super.touchesMoved(touches, with: event)
print("moved", self.state.rawValue)
}
}

class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
let g = MyRecognizer(target: self, action: #selector(grFired))
self.view.addGestureRecognizer(g)
// Do any additional setup after loading the view, typically from a nib.
}

@objc func grFired(_ g:UIGestureRecognizer) {
print("here", g.state.rawValue)
}

}

关于ios - UIGestureRecognizer 子类——touchesMoved 自动改变状态为.changed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45467029/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com