加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

objective-c – 是否可以在使用UICollectionViewFlowLayout的UIC

发布时间:2020-12-16 07:49:25 所属栏目:百科 来源:网络整理
导读:说我有一个UICollectionView使用UICollectionViewFlowLayout和以下格式: 1 2 34 5 67 8 9 我触摸单元格9并将其拖动到单元格6上,将单元格9移动到单元格6的单元格和单元格6到单元格9的位置: 1 2 34 5 97 8 6 有没有办法轻松移动和动画单元格6到单元格9以及移
说我有一个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];
}];

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读