在 Swift 中更改 tvOS 中 collectionView 单元格的 zPosition

Changing the zPosition of the collectionView cell in tvOS in Swift

我正在尝试通过如下所示的代码设置 zPosition 将聚焦的 collectionView 单元格置于最前面。但这似乎没有任何效果。请告诉我正确的步骤。

 func collectionView(_ collectionView: UICollectionView, didUpdateFocusIn context: UICollectionViewFocusUpdateContext, with coordinator: UIFocusAnimationCoordinator) {
        if let prevIndexPath = context.previouslyFocusedIndexPath, let cell = collectionView.cellForItem(at: prevIndexPath) {
            cell.contentView.layer.zPosition = 0
        }
        
        if let indexPath = context.nextFocusedIndexPath,
            let cell = collectionView.cellForItem(at: indexPath) {
            cell.contentView.layer.zPosition = 1.0
            collectionView.scrollToItem(at: indexPath, at: [.centeredHorizontally, .centeredVertically], animated: true)
        }
    }

由于我试图在 collectionViewCell 中显示图像,我们可以更改 adjustsImageWhenAncestorFocused 属性 而不是设置 zPosition。我找到了相同的解决方案。

我必须如下设置 imageview 的 adjustsImageWhenAncestorFocused 属性 :

func collectionView(_ collectionView: UICollectionView, didUpdateFocusIn context: UICollectionViewFocusUpdateContext, with coordinator: UIFocusAnimationCoordinator) {
        if let prevIndexPath = context.previouslyFocusedIndexPath, let cell = collectionView.cellForItem(at: prevIndexPath) {
                       cell.imageView.adjustsImageWhenAncestorFocused = false
        }
        
        if let indexPath = context.nextFocusedIndexPath,
            let cell = collectionView.cellForItem(at: indexPath) {
                       cell.imageView.adjustsImageWhenAncestorFocused = true
            collectionView.scrollToItem(at: indexPath, at: [.centeredHorizontally, .centeredVertically], animated: true)
        }
    }