【问题标题】:How to move the focus from a button to another in tvOS?如何在 tvOS 中将焦点从一个按钮移动到另一个按钮?
【发布时间】:2015-10-12 12:58:05
【问题描述】:

我是 tvOS 的新手。我想要一个标准按钮,一旦被按下,它就会将焦点移动到另一个标准按钮,我该怎么做(如果可能的话)?

【问题讨论】:

    标签: menu focus tvos apple-tv


    【解决方案1】:

    首先使用自定义属性覆盖 viewController 中的 preferredFocusedView

    var myPreferredFocusedView:UIView?
    override var preferredFocusedView: UIView? {
        return myPreferredFocusedView:UIView
    }
    

    然后,在您的按钮回调中,将myPreferredFocusedView 设置为您应该获得焦点的下一个首选按钮。之后,直接请求焦点更新:

    func buttonCallback(){
        myPreferredFocusedView = button2 // button2 is an example 
    
        /*
            Trigger the focus system to re-query the view hierarchy for preferred
            focused views.
        */
        setNeedsFocusUpdate()
        updateFocusIfNeeded()
    }
    

    这应该将焦点系统更新到您的另一个按钮。

    【讨论】:

    • 非常感谢安托万。它工作正常,我现在唯一遇到的问题是,当离开视图然后回来时,焦点丢失了,我需要使用箭头键(或在遥控器上拖动)来让任何按钮获得焦点。我将尝试对此进行更多研究...
    • 您可以使用相同的代码。只需确保您设置了 preferredFocusedView 并在 viewWillAppear 请求焦点更新
    • @Antoine 你能告诉我在 tvOS 中以编程方式在 10 秒后自动将焦点标签移动到 AVPlayerViewController。
    • 我试图将焦点标签移动到 AVPlayerViewController 以覆盖 preferredFocusedView 方法,但无法以编程方式移动。
    【解决方案2】:

    另一种选择是在 buttonA(例如)获得焦点时执行以下操作:

    buttonB.setNeedsFocusUpdate()
    viewController.setNeedsFocusUpdate()
    

    这仅在它们在视图层次结构中处于同一级别时才有效。如果没有,那么您可能需要将调用链接到 setNeedsFocusUpdate 一直到层次结构或其他东西......

    Read more here

    【讨论】:

    • 这就像一个魅力。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2020-03-03
    • 1970-01-01
    • 1970-01-01
    • 2016-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多