为项目的高度变化设置动画时,Collectionview performBatchUpdates 问题

Issue on Collection View performBatchUpdate when animating a height change of an Item

当使用 performBatchUpdates 方法为 CollectionViewCell 上的高度变化设置动画时会出现问题,因为这会导致出现在屏幕上的新项目在动画播放之前不在屏幕上 运行。在这种情况下,最初位于屏幕外的项目出现在它们的最终位置,而不是在那里进行动画处理。

我找到了这个解决方法来避免此 Github 页面上的这种不良行为。

这个技巧是通过在 运行 动画之前更改 CollectionView 的高度并在 performBatchUpdate 方法的完成块上将其设置回原始值来实现的。

代码可能如下所示:

frameUpdate.size.height += kExpandedHeight - kCollapsedHeight;
collectionView.frame = frameUpdate;

[collectionView performBatchUpdates:^{
     _cells[indexPath.item] = @(newHeight);
 } completion:^(BOOL finished) {
     frameUpdate.size.height -= kExpandedHeight - kCollapsedHeight;
     collectionView.frame = frameUpdate;
 }];

有关此解决方法的完整项目示例,请转到 Github 页面

https://github.com/jpsim/UICollectionView-Animation-Bug

感谢分享 JP Simard!