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