如何在Swift中检测Apple TV上的按钮突出显示

Ada*_*ex3 2 xcode uibutton apple-tv swift tvos

我正在创建一个简单的应用程序,当用户将鼠标悬停在按钮上时会向用户显示一些信息.我已经到处寻找答案,但似乎没有人想知道这一点.我知道可以检测按钮亮点,因为我在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?"

 }
Run Code Online (Sandbox Code Playgroud)

我知道只是创造一个IBAction func不会做的伎俩,但我只是用它作为我想做的一个例子.

那么有没有办法检测按钮高亮/按钮悬停以及如何?

提前致谢.任何帮助表示赞赏.

Chr*_*ris 6

通过悬停,我认为你的意思是"按钮是焦点".有两种方法可以判断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")
   }
}
Run Code Online (Sandbox Code Playgroud)

或者在自定义UIView(即自定义UIButton)中,您可以查看isFocused属性.

override func didUpdateFocus(in context: UIFocusUpdateContext, with coordinator: UIFocusAnimationCoordinator) {
    print(isFocused)
}
Run Code Online (Sandbox Code Playgroud)