2017-07-07 51 views
0

我正在製作一個簡單的應用程序,它將鼠標懸停在按鈕上時向用戶顯示一些信息。我已經找遍了所有的答案,但似乎沒有人想過這件事。我知道有可能檢測到按鈕亮點,因爲我在我的Apple TV上下載的某些應用程序中看到了它。這基本上就是我的目標:如何在Swift中檢測Apple TV上的按鈕高亮

@IBAction func firstButton(_ sender: Any) { 
//This function would be called when the first button is highlighted/hovered over 

label.text = "Hi there" 

} 

@IBAction func secondButton(_ sender: Any) { 
//This function would be called when the second button is highlighted/hovered over 

label.text = "How are you?" 

} 

我知道,只是創建一個IBAction func不會做的伎倆,但我只是用這個作爲什麼我想要做的一個例子。

那麼有沒有辦法檢測按鈕突出顯示/按鈕懸停和如何?

在此先感謝。任何幫助表示讚賞。

回答

1

通過懸停,我認爲你的意思是「焦點按鈕」。有幾種方法可以判斷UI元素是否焦點。

對於UIViewController,您可以覆蓋didUpdateFocus,它提供了關於焦點引擎中發生的情況的上下文。

override func didUpdateFocus(in context: UIFocusUpdateContext, with coordinator: UIFocusAnimationCoordinator) { 
    if context.nextFocusedView == myButton { 
     print("My button is about to be focused") 
    } 
    else { 
     print("My button is NOT in focus") 
    } 
} 

或自定義UIView(即自定義UIButton),您可以檢查isFocused屬性。

override func didUpdateFocus(in context: UIFocusUpdateContext, with coordinator: UIFocusAnimationCoordinator) { 
    print(isFocused) 
}