问题看起来像这样:http://i.imgur.com/5iaAiGQ.mp4 https://i.imgur.com/5iaAiGQ.mp4(红色是cell.contentView的颜色)
这是代码:https://github.com/nezhyborets/UICollectionViewContentsAnimationProblem https://github.com/nezhyborets/UICollectionViewContentsAnimationProblem
当前状态:UICollectionViewCell 的 contentView 的内容不会随着 contentView 框架的更改而动画化。它立即获取大小而无需动画。
执行任务时遇到的其他问题:contentView 也没有随着单元格的帧更改而动画化,直到我在 UICollectionViewCell 子类中执行此操作:
override func awakeFromNib() {
super.awakeFromNib()
//Because contentView won't animate along with cell
contentView.frame = bounds
contentView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
}
其他注意事项:下面是单元格大小动画涉及的代码
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
self.selectedIndex = indexPath.row
collectionView.performBatchUpdates({
collectionView.reloadData()
}, completion: nil)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let isSelected = self.selectedIndex == indexPath.row
let someSize : CGFloat = 90 //doesn't matter
let sizeK : CGFloat = isSelected ? 0.9 : 0.65
let size = CGSize(width: someSize * sizeK, height: someSize * sizeK)
return size
}
使用时我得到相同的结果collectionView.setCollectionViewLayout(newLayout, animated: true)
,并且使用时根本没有动画collectionView.collectionViewLayout.invalidateLayout()
代替reloadData() inside batchUpdates
.
UPDATE当我打印时imageView.constraints
集合视图内部willDisplayCell
方法,它打印空数组。
func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
for view in cell.contentView.subviews {
print(view.constraints)
}
//Outputs
//View: <UIImageView: 0x7fe26460e810; frame = (0 0; 50 50); autoresize = RM+BM; userInteractionEnabled = NO; layer = <CALayer: 0x608000037280>>
//View constraints: []
}