我的目标,同时拥有:
- 一个处理事件的 UIButton (
.touchDown
) - 层次结构中的另一个视图(即:super)接收
touchBegan/Moved/Ended/Cancelled
.
我想要那个事件,因为我需要触摸力和其他东西来进行一些计算
在upper/super 视图中,我覆盖了touchesBegan 和friends,这样我就可以获得forces 和东西。
但是,基本上, UIButton 不会转发触摸事件,因此(在此示例中)我扩展了 UIButton (在我的代码中我扩展了一个子类〜但这不会改变问题)并覆盖 touchesBegan 和朋友,并添加next?.touchesBegan(...)
给它。
什么有效:
touchesBegan(...)
正确转发到超级视图
什么不起作用:
touchesMoved(...)
只转发一次它的super
观点。(即使按钮touchesMoved
被调用,但next?
不是nil
touchesEnded(...)
之前调用过a 时不会调用touchesMoved(...)
(如果您跟随,则只有一次 touchesMoved(...) 调用)。又next?
不是nil
// One of the overrided UIButton touches event
extension UIButton {
open override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
print("Button: touches began - ")
super.touchesBegan(touches, with: event)
next?.touchesBegan(touches, with: event)
if next == nil { print("next was nil!") }
print("Button: touches began - end\n")
}
}
// One of the overrided ViewController touches event
// (which is only called once for touchesMoved, and then touchesEnded not called)
extension ViewController {
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
print("ViewController: touches began - ")
super.touchesBegan(touches, with: event)
print("ViewController: touches began - end\n")
}
}
git clone git@bitbucket.org:5t4rrk/problemtouchmovedonlyonce.git
如果有人对为什么会出现这种行为有任何见解,请告诉我\o/