我有一个分层在父 View 上的 subview 。两者都附加了 touchUpInside
监听器。当用户按下 child 时,我希望 child 和父 View 都响应此触摸事件。当用户只按下父级时,我只希望父级监听器触发。
我该怎么做?
我已经尝试在我的 subview 中覆盖以下方法,但是在这里返回 false 会完全忽略 subview 中的事件。相反,如果我在子进程中处理触摸事件,则父进程会忽略它。
override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
return false
}
最佳答案
如果你有两个 View 的子类,你可以覆盖 UIView touchesBegan方法。如果您调用 super touchesBegan
方法,那么当事件沿着响应者链向上传播时,这应该会产生您想要的结果。 (即如果用户点击 subview , subview 和父 View 的触摸事件都会响应)
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesBegan(touches, with: event)
print("TOUCHED")
}
关于ios - 响应 child 和 parent 的触摸事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55340641/