Ios 在Swift中更改tvOS中collectionView单元格的位置
我正试图通过设置zPosition(如下面所示的代码中所示)将聚焦的collectionView单元格带到前面。但这似乎没有任何效果。请告诉我做这件事的正确程序Ios 在Swift中更改tvOS中collectionView单元格的位置,ios,swift,uicollectionview,uicollectionviewcell,zposition,Ios,Swift,Uicollectionview,Uicollectionviewcell,Zposition,我正试图通过设置zPosition(如下面所示的代码中所示)将聚焦的collectionView单元格带到前面。但这似乎没有任何效果。请告诉我做这件事的正确程序 func collectionView(_ collectionView: UICollectionView, didUpdateFocusIn context: UICollectionViewFocusUpdateContext, with coordinator: UIFocusAnimationCoordinator) {
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中显示图像,因此不必设置zPosition,我们可以更改adjustsImageWhenAncestorFocused属性。我找到了同样的解决办法 我必须按如下方式设置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)
}
}