tvOS UICollectionViewFocusUpdateContext 在快速滚动时选择多个项目

tvOS UICollectionViewFocusUpdateContext selects multiple items on fast scrolling

在我的 SpriteKit 项目中,我使用 UICollectionView 来创建字符 select 菜单。它是一个非常简单的 CollectionView,在 1 行中有 8 个单元格。每个单元格只有一个角色的 imageView。

在 tvOS 上,当我设置

时我没有使用 automatic/default 对焦系统
imageView.adjustsImageWhenAncestorFocused = true

因为我不喜欢用它得到的阴影效果。

所以我正在使用

手动进行对焦
func collectionView(_ collectionView: UICollectionView, didUpdateFocusIn context: UICollectionViewFocusUpdateContext, with coordinator: UIFocusAnimationCoordinator) {

    if let previousIndexPath = context.previouslyFocusedIndexPath, let cell = collectionView.cellForItem(at: previousIndexPath) {
        scaleDown(cell: cell, coordinator: coordinator)
    }

    if let nextIndexPath = context.nextFocusedIndexPath, let cell = collectionView.cellForItem(at: nextIndexPath) {
        scaleUp(cell: cell, coordinator: coordinator)
    }
}

ScaleUp 和 ScaleDown 是两个简单的方法,它们将使用协调器来 动画单元格的焦点外观。这里没什么特别的。

当我从慢速滚动到中速滚动时,一切都很好。但是,当我进行非常快速的滚动时,它基本上从第一个单元格滚动到最后一个单元格,有时 selects 多个项目。我最终聚焦了 2-3 个细胞,而不是只有 1 个,似乎聚焦系统跟不上。

注意:如果我尝试只更改单元格的 imageView 的比例而不是整个单元格的比例,它看起来会好得多,但我仍然能够偶尔重现该问题。

有没有办法完全避免这种情况?

通过将焦点代码更改为此

来修复它
func collectionView(_ collectionView: UICollectionView, didUpdateFocusIn context: UICollectionViewFocusUpdateContext, with coordinator: UIFocusAnimationCoordinator) {

    if let previousCell = context.previouslyFocusedView as? UICollectionViewCell {
        scaleDown(cell: previousCell, coordinator: coordinator)
    }

    if let nextCell = context.nextFocusedView as? UICollectionViewCell {
        scaleUp(cell: nextCell, coordinator: coordinator)
    }
}