【问题标题】:How to move focus at button in collectionview cell in tvos application?如何在 tvos 应用程序的 collectionview 单元格中的按钮上移动焦点?
【发布时间】:2016-11-25 04:36:31
【问题描述】:

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

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

【问题讨论】:

  • 焦点是什么意思?
  • 焦点意味着在 tv os 应用程序中,只要焦点移动,您就可以点击按钮。
  • 更好地使用集合视图单元格的交互。
  • @Karthick 是的,我可以与集合视图单元格进行交互,但在我的应用程序中,按钮位于 collectionViewCell 中,所以我如何单击它们?
  • 您的收藏视图单元格中有哪些元素?

标签: ios tvos apple-tv tvos9.1 tvos10


【解决方案1】:

我可以通过在 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
}

您可以在此链接中查看更多信息: enter link description here.

【讨论】:

  • 它没有像预期的那样工作,首先它聚焦第一个按钮,然后是第二个按钮,最后是单元格......这不是正确的用户体验。
【解决方案2】:

我认为此页面将指导您实现您想要的。 https://developer.apple.com/library/content/documentation/General/Conceptual/AppleTV_PG/WorkingwiththeAppleTVRemote.html#//apple_ref/doc/uid/TP40015241-CH5-SW4

它很好地解释了焦点引擎将如何决定哪个对象应该获得下一个焦点。下面是above link.的详细解释。

下面是一个例子,展示了如何确定焦点:

  1. 焦点引擎向根窗口询问它的preferredFocusedView, 它返回它的根视图控制器的preferredFocusedView 目的。
  2. 根视图控制器,一个选项卡视图控制器,返回它的 选择视图控制器的 preferredFocusedView 对象。
  3. 选择视图控制器会覆盖它的preferredFocusedView 方法以返回一个特定的UIButton 实例。
  4. UIButton 实例返回 self(默认值),并且是可聚焦的,因此它被聚焦引擎选择为下一个聚焦视图。

您必须覆盖 UIView 或 UIViewController 的 preferredFoucsedView 属性。

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

感谢Slayter's的回答

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-07
    相关资源
    最近更新 更多