IOS触摸位置,而不uigesturerecognizer
问题描述:
如何在一个UIWindow或UIView的获得触摸位置没有uigesturerecognizer, 目的是在极短的延迟得到触摸位置作为调度员可延缓触摸点火事件IOS触摸位置,而不uigesturerecognizer
答
您需要继承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
。
答
覆盖以下方法在您的UIView子类中。
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let point = touches.first?.location(in: self)
print("location: ", point ?? CGPoint.zero)
}
+0
当我重写touchesBegan时,我仍然需要等到调度员触发touchesBegan事件后,在我的个人测试中,它会以50Hz的频率被触发,并有一些延迟,但我想要得到100Hz的低延迟 – user818117
当我重写的touchesBegan我还需要等到调度触发的touchesBegan事件,在我个人的测试中,它与50Hz的发射,具有一定的延迟,但我想要得到它的100Hz,低延迟 – user818117
据据我所知,iOS SDK不提供更快的速度。也许如果你改为“UIWindow”的子类(视图层次结构的根对象),它会稍微调用一些? –
也许重写'UIApplication.sendEvent()'?想不到别的。无论哪种方式,'touchesBegan(...)的速度足够快,以至于人类用户的交互感觉很自然。我不知道你的应用程序试图做什么,需要在这种实时条件下检测水龙头, –