如何在TVOS应用程序中的CollectionView单元格中移动焦点



我在Apple TV应用程序上工作。在我的应用程序中,我制作了一个具有收集视图的屏幕。在这种情况下,我能够在收集视图单元格上移动焦点,但无法将焦点移至收集查看单元格中的按钮,所以任何人都可以帮助我解决此问题吗?

如果有人知道这个答案,请给我一个答案。

我能够通过在CollectionViewCell子类中添加以下方法来解决此问题。

在CollectionViewCell子类中

override var preferredFocusEnvironments: [UIFocusEnvironment]{
    // Condition
}
override func shouldUpdateFocus(in context: UIFocusUpdateContext) -> Bool {
    // Condition
}
override func didUpdateFocus(in context: UIFocusUpdateContext, with coordinator: UIFocusAnimationCoordinator) {
    // Condition
}

您可以在此链接上看到更多:在此处输入链接描述。

我认为此页面将指导实现您想要的东西。https://developer.apple.com/library/content/documentation/general/coneptual/coneptual/appletv_pg/workingwiththeappletvremote.htmll#/apple/apple_ref/doc/doc/doc/doc/uid/uid/tp40015241-ch5241-ch5-sw4

它很好地解释了焦点引擎将如何决定哪个对象应获得下一个焦点。以下是以上链接的很好的解释。

这是一个示例,显示如何确定焦点:

  1. 焦点引擎向根窗口询问其preferredfocusedview,它返回其根视图控制器的preferredFocusedView目的。
  2. root View Controller,一个选项卡视图控制器,返回其选择查看控制器的preferredFocusedView对象。
  3. SELECT VIEW CONTRAINER覆盖其PreferredFocusedView方法,以返回特定的Uibutton实例。
  4. uibutton实例返回自我(默认值),并且是可聚焦的,因此焦点引擎选择为下一个焦点视图。

您必须覆盖Uiview或UiviewController的首选foucsedview属性。

override weak var preferredFocusedView: UIView? {
    if someCondition {
        return theViewYouWant
    } else {
        return defaultView
    }
}

感谢Slayter的答案

最新更新