【发布时间】:2021-03-02 16:24:01
【问题描述】:
我的目标,同时拥有:
- 处理事件的 UIButton (
.touchDown) - 接收
touchBegan/Moved/Ended/Cancelled的层次结构中的另一个上层视图(即:super)。
我想要那个事件,因为我需要触摸力和其他东西来进行一些计算
在upper/super视图中,我覆盖了touchesBegan和friends,这样我就可以得到forces和东西了。
但是,基本上,UIButton 不会转发触摸事件,因此(在此示例中)我扩展了 UIButton(在我的代码中我扩展了一个子类〜但这不会改变问题)并覆盖 touchesBegan 和朋友,并在其中添加next?.touchesBegan(...)。
什么有效:
-
touchesBegan(...)正确转发到超级视图
什么不起作用:
-
touchesMoved(...)仅转发一次 到其super视图。 (即使按钮的touchesMoved被调用并且next?不是nil -
touchesEnded(...)是NOT CALLED,当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")
}
}
Here is an example project to show you the problem:
git clone git@bitbucket.org:5t4rrk/problemtouchmovedonlyonce.git
如果有人对为什么会出现这种行为有任何见解,请告诉我\o/
【问题讨论】:
标签: ios swift uibutton touchesmoved touchesended