动画UICollectionView单元格大小更改并重新定位周围的单元格
目标:
为单元格高度的变化设置动画并重新定位周围的单元格。
To animate the change of a cell's height and reposition surrounding cells.
场景:
集合视图中的某些单元格会加载远程图像。最初,这些单元格的大小是静态的,并显示了活动指示符。加载图像后,将其添加到其单元格中,并更改单元格的高度以适合照片。
Some cells in a collection view load remote images. Initially, those cells are sized statically and an activity indicator is shown. After an image is loaded, it is added to its cell and the cell's height is changed to fit the photo.
注意:
我使用 animateWithDuration
为单元格的帧更改设置动画。这种方法很好,除了增加的单元格大小与下面的单元格重叠。在调整目标单元格大小并更新 sizeForItemAtIndexPath
返回的大小后,我盲目地尝试调用 collectionView.collectionViewLayout invalidateLayout
成功。
I am animating the cell's frame change with animateWithDuration
. This works fine, except an increased cell size has it overlapping the cells below. I've blindly tried calling collectionView.collectionViewLayout invalidateLayout
after resizing the target cell and updating the size returned by sizeForItemAtIndexPath
with no success.
有什么建议吗?谢谢!
示例代码:
基本上你需要做三个步骤
Basically you need to do three steps
1.验证collectionViewLayout
1.invalidate the collectionViewLayout
2.执行批量更新:
...
//invalidate layout
[collectionView performBatchUpdates:^{
// make your cell at indexPath return a new size
} completion:^(BOOL finished) {
}];
3.在 sizeForItemAtIndexpath
返回新尺寸