2017-07-07 53 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) 
}