美文网首页iOS Developer
对UICollectionView的拖拽排序

对UICollectionView的拖拽排序

作者: 小小小z_逍遥 | 来源:发表于2017-09-19 16:55 被阅读275次

    最近工作中封装的论证,使用很方便,在这里分享个大家;支持长按 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的拖拽排序使用。

    相关文章

      网友评论

        本文标题:对UICollectionView的拖拽排序

        本文链接:https://www.haomeiwen.com/subject/oihwsxtx.html