美文网首页
collectionview长按更改cell位置

collectionview长按更改cell位置

作者: guoguojianshu | 来源:发表于2020-11-09 10:43 被阅读0次

直接看代码

/// 是否移动
@property (nonatomic, assign) BOOL isBeganMove;

-(void)setXSImageArray:(NSArray<UIImage *> *)XSImageArray{
    _XSImageArray = XSImageArray;
    [self.XSCollectionView reloadData];
}


#pragma mark ___返回collectionview的每组中row的点击事件___
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
  XSAddPicCollectionViewCell * XSCell =  (XSAddPicCollectionViewCell*)[collectionView cellForItemAtIndexPath:indexPath];
    if ([XSCell.XSImageView.image isEqual:[UIImage imageNamed:@"XS_HomeTrendAdd"]]) {
        if (self.XSCollectionBlock) {
            self.XSCollectionBlock(indexPath);
        }
    }else{
        if (self.XSCellDidSelectBlock) {
            self.XSCellDidSelectBlock(indexPath);
        }
    }
    
}

-(void)moveCollectionViewCell:(UILongPressGestureRecognizer *)gesture{
    switch (gesture.state) {
        case UIGestureRecognizerStateBegan:
            if (!self.isBeganMove) {
                self.isBeganMove = YES;
                NSIndexPath * selectedIndexPath = [self.XSCollectionView indexPathForItemAtPoint:[gesture locationInView:self.XSCollectionView]];
                NSLog(@"点击cell的位置%@",selectedIndexPath);

                [self.XSCollectionView beginInteractiveMovementForItemAtIndexPath:selectedIndexPath];
            }
            break;
        case UIGestureRecognizerStateChanged:{
            NSIndexPath * desIndex = [self.XSCollectionView indexPathForItemAtPoint:[gesture locationInView:self.XSCollectionView]];
            if (desIndex.item < self.XSImageArray.count) {
                
                [self.XSCollectionView updateInteractiveMovementTargetPosition:[gesture locationInView:self.XSCollectionView]];
            }else{
                [self.XSCollectionView cancelInteractiveMovement];

            }
            NSLog(@"移动到的位置%@",desIndex);
        }
            break;
        case UIGestureRecognizerStateEnded:{
            self.isBeganMove = false;
//            结束移动
            [self.XSCollectionView endInteractiveMovement];
        }
            break;
        default:
            [self.XSCollectionView cancelInteractiveMovement];
            break;
    }
}
-(BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath{
    return indexPath.item < self.XSImageArray.count;
}

-(void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
    if (self.XSCellMoveBlock) {
        self.XSCellMoveBlock(sourceIndexPath, destinationIndexPath);
    }
}



移动cell的block的回调处理
XSCell.XSCellMoveBlock = ^(NSIndexPath * _Nonnull sourchIndex, NSIndexPath * _Nonnull desIndex) {
           UIImage * selectImage = XSWeakself.XSLoactionImageArray[sourchIndex.item];
            [XSWeakself.XSLoactionImageArray removeObjectAtIndex:sourchIndex.item];
            [XSWeakself.XSLoactionImageArray insertObject:selectImage atIndex:desIndex.item];
            
            id selectAsset = XSWeakself.XSSelectAssetsArray[sourchIndex.item];
            [XSWeakself.XSSelectAssetsArray removeObjectAtIndex:sourchIndex.item];
            [XSWeakself.XSSelectAssetsArray insertObject:selectAsset atIndex:desIndex.item];
            [XSWeakself.XSTableView reloadData];
        };

相关文章

网友评论

      本文标题:collectionview长按更改cell位置

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