2017-08-30 45 views

回答

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

+0

当我重写的touchesBegan我还需要等到调度触发的touchesBegan事件,在我个人的测试中,它与50Hz的发射,具有一定的延迟,但我想要得到它的100Hz,低延迟 – user818117

+0

据据我所知,iOS SDK不提供更快的速度。也许如果你改为“UIWindow”的子类(视图层次结构的根对象),它会稍微调用一些? –

+0

也许重写'UIApplication.sendEvent()'?想不到别的。无论哪种方式,'touchesBegan(...)的速度足够快,以至于人类用户的交互感觉很自然。我不知道你的应用程序试图做什么,需要在这种实时条件下检测水龙头, –

0

覆盖以下方法在您的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