繁体   English   中英

如何在Swift中检测Apple TV上的按钮高亮

[英]How to detect button highlight on Apple TV in 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就不会成功,但我只是以此为例来说明自己想要做的事情。

那么,有没有一种方法可以检测按钮高亮/按钮悬停以及如何检测?

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

悬停时 ,我认为您的意思是“是关注的按钮”。 有几种方法可以确定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)
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM