说我有一个UICollectionView使用UICollectionViewFlowLayout和以下格式:
1 2 3 4 5 6 7 8 9
我触摸单元格9并将其拖动到单元格6上,将单元格9移动到单元格6的单元格和单元格6到单元格9的位置:
1 2 3 4 5 9 7 8 6
有没有办法轻松移动和动画单元格6到单元格9以及移动和动画单元格9到单元格6的位置?还是我必须将UICollectionViewLayout子类化?如果我必须对UICollectionViewLayout进行子类化,那该怎么做?我已经尝试明确设置单元格的中心
– (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
但没有成功.
解决方法
经过很多的敲击,我发现的解决方案是使用moveItemAtIndexPath:toIndexPath里面的performBatchUpdates:completion.根据
documentation for performBatchUpdates:完成:
You can use this method in cases where you want to insert,delete,reload or move cells around the collection view in one single animated operation,as opposed to in several separate animations. Use the blocked passed in the updates parameter to specify all of the operations you want to perform.
所以,我本来做的是什么
[self performBatchUpdates:^{ [self moveItemAtIndexPath:fromIp toIndexPath:toIp]; [self moveItemAtIndexPath:toIp toIndexPath:fromIp]; } completion:^(BOOL finished) { // update data source here // e.g [dataSource exchangeObjectAtIndex:fromIp.row withObjectAtIndex:toIp.row]; }];