简体   繁体   English

如何将焦点移到tvos应用程序的collectionview单元中的按钮上?

[英]How to move focus at button in collectionview cell in tvos application?

I am working on apple TV application. 我正在研究Apple TV应用程序。 In my app, I have made a screen which has a collection view. 在我的应用程序中,我制作了一个具有收藏夹视图的屏幕。 In that case, I am able to move focus at collection view cell but not able to move focus to the button which is in collection view cell so can anyone help me to solve this issue ? 在那种情况下,我可以将焦点移到集合视图单元格上,但是不能将焦点移到集合视图单元格中的按钮上,那么有人可以帮助我解决此问题吗?

please give me an answer if anyone knows this answer. 如果有人知道这个答案,请给我一个答案。

I am able to solve this problem by adding below methods in collectionViewCell subclass. 我可以通过在collectionViewCell子类中添加以下方法来解决此问题。

In CollectionViewCell Subclass 在CollectionViewCell子类中

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

you can see more at this link: enter link description here . 您可以在此链接中看到更多信息: 在此处输入链接描述

I think this page will guide to achieve what you want. 我认为此页面将指导您实现所需的内容。 https://developer.apple.com/library/content/documentation/General/Conceptual/AppleTV_PG/WorkingwiththeAppleTVRemote.html#//apple_ref/doc/uid/TP40015241-CH5-SW4 https://developer.apple.com/library/content/documentation/General/Conceptual/AppleTV_PG/WorkingwiththeAppleTVRemote.html#//apple_ref/doc/uid/TP40015241-CH5-SW4

It give good explaination of how focus engine will decide which object should get next focus. 它很好地说明了焦点引擎如何决定哪个对象应该获得下一个焦点。 Below is great explanation step by step at above link. 下面是以上链接的逐步详细说明

Here is an example showing how focus might be determined: 这是显示如何确定焦点的示例:

  1. The focus engine asks the root window for its preferredFocusedView, which returns its root view controller's preferredFocusedView object. 焦点引擎向根窗口询问其preferredFocusedView,它返回其根视图控制器的preferredFocusedView对象。
  2. The root view controller, a tab view controller, returns its select view controller's preferredFocusedView object. 根视图控制器(一个选项卡视图控制器)返回其选择视图控制器的preferredFocusedView对象。
  3. The select view controller overrides its preferredFocusedView method to return a specific UIButton instance. 选择视图控制器重写其preferredFocusedView方法以返回特定的UIButton实例。
  4. The UIButton instance returns self(the default), and is focusable, so it is chosen by the focus engine as the next focused view. UIButton实例返回self(默认值),并且是可聚焦的,因此焦点引擎将其选择为下一个焦点视图。

You have to override the preferredFoucsedView property of your UIView or UIViewController. 您必须重写UIView或UIViewController的preferredFoucsedView属性。

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

Thanks to Slayter's answer 感谢Slayter的回答

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

相关问题 在 Swift 中更改 tvOS 中 collectionView 单元格的 zPosition - Changing the zPosition of the collectionView cell in tvOS in Swift tvos UICollectionView失去了对前一个Focus Cell的关注 - tvos UICollectionView lose focus to previous Focus Cell 无法将焦点移至tvOS中的下一个UICollectionViewCell - Can not move focus to next UICollectionViewCell in tvOS 如何在tvOS应用程序中为collectionview的节头设置动画? - how to animate section header of collectionview in tvOS app? collectionView单元格中的按钮选择器 - selector for button inside collectionView cell 如何从嵌套的collectionView单元格传递按钮操作? - How do I pass button action from a nested collectionView cell? tvos如何始终关注特定的tableViewCell - tvos How to always focus specific tableViewCell tvOS - 使用 remembersLastFocusedIndexPath = true 在某个时间点重置 CollectionView 的焦点 [swift] - tvOS - Reset CollectionView's focus at one point of time with remembersLastFocusedIndexPath = true [swift] 如何在iOS的UITableView中将可访问性焦点从UITableViewHeader移到Cell - How to move accessibilty focus from UITableViewHeader to Cell in UITableView in iOS SWIFT:在 TVOS 中退出收集时强制聚焦引擎转到按钮 - SWIFT: Force focus engine to go to button when quitting collection in TVOS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM