swift2 - UICollectionView : Programmatically moving focus from one view to another - tvOS

标签 swift2 tvos

我正在尝试以编程方式将焦点从一个 View 移动到另一个 View ,就像单击按钮一样 我正在尝试在 Collection View 中选择一个索引。

我正在尝试复制 SIRI 的工作功能,因为 Siri 不对开发人员开放,但我正在将要通过 bonjour 播放的节目发送到应用程序,以便它播放它唯一的一点是节目,即索引路径应该是在播放视频之前集中注意力,就像 NBC 应用程序在 tvOS 中所做的那样。

我试过:

- collectionView.selectItemAtIndexPath(selectedIndexPath, animated: false, scrollPosition: .None)
       collectionView.reloadData()
       setNeedsFocusUpdate()
       collectionView.updateFocusIfNeeded()

似乎没有任何效果并将焦点转移到给定的索引,我想知道这在 tvOS 中是否可行?

我正在构建一个 tvOS 原生应用程序。

最佳答案

根据 App Programming Guide for tvOS,没有明确的方法以编程方式将焦点从一个 View 移动到另一个 View :

The Focus Engine Controls Focus

Only the focus engine can explicitly update focus, meaning there is no API for directly setting the focused view or moving focus in a certain direction. ...

The focus engine controls focus to make sure that it does not move around the screen unexpectedly, and that it behaves similarly across different applications.

但是,您可以拥有 UICollectionViewDelegate为 Collection View 的首选焦点项指定索引路径,当 Collection View 获得焦点时,它将聚焦该索引路径:

func indexPathForPreferredFocusedViewInCollectionView(_ collectionView: UICollectionView) -> NSIndexPath? {
    // Return index path for selected show that you will be playing
    return NSIndexPath(forRow: ..., inSection: ...)
}

The functionality of this delegate method is equivalent to overriding the UICollectionView class’s preferredFocusedView method in the UIFocusEnvironment protocol. If the collection view’s remembersLastFocusedIndexPath method is set to YES, this method defines the index path that gets focused when the collection view is focused for the first time.

关于swift2 - UICollectionView : Programmatically moving focus from one view to another - tvOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34598023/

相关文章:

ios - 如何从 API 检索 iPhone IDFA?

swift - 向 wit.ai 发送文本时接收结果的问题

ios - 断开连接时所有应用程序崩溃

swift - 如何从 catch 子句中的 NSError 获取 userInfo

ios - 从 UITextView Swift 2 获取文本

xcode - Applovin tvOS 库问题

swift - tvOS - pressesEnded 在启动时不起作用

ios - TableViewController 中的 SearchBar

ios - tvOS 视频 : Title metadata truncated, 图稿尺寸太小

swift - 设置图像后,UIImageView 返回到原始位置 - tvOS