2017-08-30 48 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