UICollectionCell实现拖动效果

UICollectionView实现cell拖动效果

1.在UICollectionView的视图上添加长按手势

UILongPressGestureRecognizer *longGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handlelongGesture:)];
    [self.collectionView addGestureRecognizer:longGesture];
    

2.在点击的时候,获取到当前点击的cell的indexpath,并在不同的手势状态下,进行一些判断或者添加一些约束条件【因为我的情景是cell中显示不同的服务,而最后一个cell与其他不同,不参与排序,所以我需要保证拖动的cell不可以到最后一个位置,并且最后一个cell不可以拖动】

- (void)handlelongGesture:(UILongPressGestureRecognizer *)longGesture {
    NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:[longGesture locationInView:self.collectionView]];
    UICollectionViewCell *tmpCell = [self.collectionView cellForItemAtIndexPath:indexPath];
    [tmpCell.layer shake];
    //判断手势状态
    switch (longGesture.state) {
        case UIGestureRecognizerStateBegan:{
            [tmpCell.layer playSystemShake];
            //判断手势落点位置是否在路径上
            if (indexPath == nil) {
                break;
            }
            //在路径上则开始移动该路径上的cell
            [self.collectionView beginInteractiveMovementForItemAtIndexPath:indexPath];
        }
            break;
        case UIGestureRecognizerStateChanged:
            NSLog(@"%lu",(unsigned long)_showDataArr.count);
            //判断是否落在最后的加号上
            if (indexPath.row == _showDataArr.count) {
                break;
            }
            //移动过程当中随时更新cell位置
            [self.collectionView updateInteractiveMovementTargetPosition:[longGesture locationInView:self.collectionView]];
            break;
        case UIGestureRecognizerStateEnded:
            //移动结束后关闭cell移动
            [self.collectionView endInteractiveMovement];
            break;
        default:
            [self.collectionView cancelInteractiveMovement];
            break;
    }
}

3我们还需要实现UIcollectionView的一个代理方法,并保证不参与拖动的cell不可移动

- (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath{
    //返回YES允许其item移动
    if(indexPath.row != [_showDataArr count]) {
        return YES;
    }
    return NO;
}

4实现数据的重新排序

- (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath*)destinationIndexPath {
    //取出源item数据
    id objc = [_showDataArr objectAtIndex:sourceIndexPath.item];
    //从资源数组中移除该数据
    [_showDataArr removeObject:objc];
    //将数据插入到资源数组中的目标位置上
    [_showDataArr insertObject:objc atIndex:destinationIndexPath.item];
    NTSubscribeModel *model = [NTSubscribeModel new];
    [model updateServicesOrder:_showDataArr];
    model = nil;
    
}

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

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,301评论 4 61
  • 金风送爽,秋意渐浓。9月7日上午,厦门路小学积分制管理启动大会在学校多功能厅隆重举行。平度教体局基教办王道...
    管霆阅读 3,523评论 0 0
  • 早上五点的闹钟五点半才起来,看了一集半课程,讲的长投的权益法机成本法核算,又有点犯困了,再睡了一小时上班,这会儿眼...
    老鸟是啥鸟阅读 916评论 0 1
  • 为什么安装打印机不能像电源插座那么简单 往往越便捷的操作,都能让用户快速建立起习惯,并具有很强的复制性。用户天生的...
    正能量哈哈哈阅读 1,241评论 0 0
  • 今天下午去学校开家长会,我真的没想到自己竟然会这么累,在学校的礼堂里就睡着了,可能这段时间真的是运动和学习的双向作...
    尚巾林阅读 1,332评论 0 0