首页 > 解决方案 > 如何禁用 tvOS Apple TV 中的自动焦点更改

问题描述

我有一个带有 collectionView 的详细信息页面。集合视图有一个标题视图,其中有一些按钮(播放按钮)。当页面第一次打开时,焦点将放在播放按钮上。加载标题视图后,我们将插入轨道单元格以在 collectionView 中显示相关项目。但是,每当轨道加载时,焦点就会自动移动到轨道单元。没有为collectionView做焦点处理只有一个是这段代码

func collectionView(_ collectionView: UICollectionView, canFocusItemAt indexPath: IndexPath) -> Bool {
        return true
    }

我怎样才能消除这种行为?我尝试“应该更新焦点”来获得这个焦点变化并阻止它

override func shouldUpdateFocus(in context: UIFocusUpdateContext) -> Bool {
        <#code#>
    }

但是只有当焦点更改是通过远程进行并且没有自动焦点更改(例如我的情况)时才会触发此功能现在我正在处理这种情况

override func didUpdateFocus(in context: UIFocusUpdateContext, with coordinator: UIFocusAnimationCoordinator) {
        if let headerView = getHeaderView(), let collectionViewCell = detailsCollectionView.cellForItem(at: IndexPath(item: 0, section: 0)) as? CollectionViewCell {
            let previouslyFocusedView = context.previouslyFocusedView
            let nextFocusedView = context.nextFocusedView
            if previouslyFocusedView?.isDescendant(of: headerView.playButton) ?? false, nextFocusedView?.isDescendant(of: collectionViewCell.getInnerCollectionView()) ?? false {
                cancelFocusChange(previouslyFocusedView: previouslyFocusedView)
            } 
        }
        super.didUpdateFocus(in: context, with: coordinator)
    }

但我认为这不是一个好主意。发生这种焦点变化一定是有原因的。如果有人知道这个问题的答案,请帮忙。

标签: iosswiftuicollectionviewtvosapple-tv

解决方案


推荐阅读