简体   繁体   English

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

[英]Changing the zPosition of the collectionView cell in tvOS in Swift

I am trying to bring the focused collectionView cell to the front by setting the zPosition like in the code shown below.我正在尝试通过设置 zPosition 将聚焦的 collectionView 单元格放在前面,如下面的代码所示。 But that doesn't seem to have any effect.但这似乎没有任何效果。 Please let me know the correct procedure to do it.请让我知道正确的步骤。

 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)
        }
    }

Since i am trying to show an image in the collectionViewCell, instead of setting the zPosition we can change the adjustsImageWhenAncestorFocused property.因为我试图在 collectionViewCell 中显示图像,所以我们可以更改 adjustsImageWhenAncestorFocused 属性而不是设置 zPosition。 I found the solution for the same.我找到了相同的解决方案。

I had to set the adjustsImageWhenAncestorFocused property of the imageview as follows:我必须将 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)
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM