如果没有uigesturerecognizer,如何在uiwindow或uiview上获取触摸位置, 目的是以极低的延迟获得触摸位置,因为调度员可以延迟触发射击事件
答案 0 :(得分:0)
您需要继承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
。
答案 1 :(得分:0)
在 UIView 子类中覆盖以下方法。
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let point = touches.first?.location(in: self)
print("location: ", point ?? CGPoint.zero)
}