【问题标题】:Changing the zPosition of the collectionView cell in tvOS in Swift在 Swift 中更改 tvOS 中 collectionView 单元格的 zPosition
【发布时间】:2021-01-07 06:48:06
【问题描述】:

我正在尝试通过设置 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)
        }
    }

【问题讨论】:

    标签: ios swift uicollectionview uicollectionviewcell zposition


    【解决方案1】:

    由于我试图在 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)
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-11
      • 1970-01-01
      • 2021-12-10
      • 1970-01-01
      • 2022-07-05
      相关资源
      最近更新 更多