【发布时间】:2017-04-02 12:16:47
【问题描述】:
问题看起来像这样:http://i.imgur.com/5iaAiGQ.mp4 (红色是cell.contentView的颜色)
这里是代码:https://github.com/nezhyborets/UICollectionViewContentsAnimationProblem
当前状态: UICollectionViewCell 的 contentView 的内容不会随着 contentView 帧的变化而动画。它立即获得大小而无需动画。
执行任务时遇到的其他问题: 在我在 UICollectionViewCell 子类中执行此操作之前,contentView 也没有与单元格的框架更改一起动画:
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 时根本没有动画。
更新
当我在 UICollectionView 的 willDisplayCell 方法中打印 imageView.constraints 时,它会打印空数组。
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: []
}
【问题讨论】:
标签: ios animation uicollectionview uicollectionviewcell