tvOS UICollectionViewFocusUpdateContext在快速滚动

时间:2016-11-01 14:10:38

标签: uicollectionview focus tvos

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

在tvOS上,当我设置

时,我没有使用自动/默认焦点系统
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是两个使用协调器的简单方法 动画细胞的焦点外观。没什么好看的。

当我快速滚动到中等速度时,一切都运行良好。然而,当我进行一个非常快速的滚动时,它基本上从第一个单元格到最后一个单元格,它有时会选择多个项目。我最终得到的焦点是2-3个细胞,而不是只有1个,看起来焦点系统跟不上。

注意:如果我只是尝试改变单元格的imageView而不是整个单元格的比例,那么它似乎要好得多,但是我偶尔也能够复制这个问题。

无论如何,我完全可以避免这种情况吗?

1 个答案:

答案 0 :(得分:0)

通过将焦点代码更改为此

来解决此问题
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)
    }
}