对UICollectionView的拖拽排序

最近工作中封装的论证,使用很方便,在这里分享个大家;支持长按 cell单元格拖动排序。

这是根据一个前辈的思路。我加了一点点改进。话不多说,进入正题。

先来Gif镇楼:


CollectionView拖拽排序.gif

1.这个轮子主要是使用长按手势喝拖动手势来实现拖动交换的。

使用KVO监控,正确的添加手势。通过手势代理区分长按手势和滑动手势,链接在这里,因为使用比较简单,只放了代码,没有写demo。
这是分类里面的几个代理方法。根据名字就能明白各个方法等作用,只需要设置好CollectionView的代理方法后可以使用

@protocol HBCollectionViewMoveDataSource <UICollectionViewDataSource>

@optional

- (BOOL)collectionView:(UICollectionView *)collectionView
canMoveItemAtIndexPath:(NSIndexPath *)indexPath;

- (BOOL)collectionView:(UICollectionView *)collectionView
       itemAtIndexpath:(NSIndexPath *)sourceIndexPath
    canMoveToIndexPath:(NSIndexPath *)destinationIndexPath;

- (void)collectionView:(UICollectionView *)collectionView
       itemAtIndexPath:(NSIndexPath *)sourceIndexPath
   willMoveToIndexPath:(NSIndexPath *)destinationIndexPath;

- (void)collectionView:(UICollectionView *)collectionView
       itemAtIndexPath:(NSIndexPath *)sourceIndexPath
   didMoveToIndexPath:(NSIndexPath *)destinationIndexPath;

@end

@protocol HBCollectionViewDelegateMoveFlowLayout <UICollectionViewDelegateFlowLayout>

@optional

- (void)collectionView:(UICollectionView *)collectionView
                layout:(UICollectionViewLayout *)collectionViewLayout
willBeginDraggingItemAtIndexPath:(NSIndexPath *)indexPath;

- (void)collectionView:(UICollectionView *)collectionView
                layout:(UICollectionViewLayout *)collectionViewLayout
didBeginDraggingItemAtIndexPath:(NSIndexPath *)indexPath;

- (void)collectionView:(UICollectionView *)collectionView
                layout:(UICollectionViewLayout *)collectionViewLayout
willEndDraggingItemAtIndexPath:(NSIndexPath *)indexPath;

- (void)collectionView:(UICollectionView *)collectionView
                layout:(UICollectionViewLayout *)collectionViewLayout
didEndDraggingItemAtIndexPath:(NSIndexPath *)indexPath;

@end

使用非常简单,至于要把项目中的UICollectionViewFlowLayout替换成HBCollectionViewMoveFlowLayout就行.
其他代码就不贴了,可以去GitHub上下载,最后,在送上传送门.
后面我会想想封装TableView的拖拽排序使用。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容