【发布时间】:2017-08-30 09:41:07
【问题描述】:
如何在没有 uigesturerecognizer 的情况下在 uiwindow 或 uiview 上获取触摸位置, 目的是以极低的延迟获取触摸位置,因为调度程序可以延迟触摸触发事件
【问题讨论】:
如何在没有 uigesturerecognizer 的情况下在 uiwindow 或 uiview 上获取触摸位置, 目的是以极低的延迟获取触摸位置,因为调度程序可以延迟触摸触发事件
【问题讨论】:
您需要继承UIView 并覆盖UIResponder(UIView 的超类)方法touchesBegan(_:with:)。有关方法here,请参阅文档。
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
// Get the touch location in THIS VIEW's coordinate system:
let locationInThisView = touch.location(in: self)
// Get the touch location in the WINDOW's coordinate system
let locationInWindow = touch.location(in: nil)
}
}
另外,请确保您的视图将属性isUserInteractionEnabled 设置为true。
【讨论】:
UIWindow 子类化(视图层次结构的根对象),它会被调用得稍早一些?
UIApplication.sendEvent()?想不出别的了。无论哪种方式,touchesBegan(... 都足够快,以至于交互对人类用户来说是自然的。我想知道您的应用正在尝试做什么,需要在这种实时条件下检测点击。
在您的 UIView 子类中覆盖以下方法。
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let point = touches.first?.location(in: self)
print("location: ", point ?? CGPoint.zero)
}
【讨论】: