美文网首页
2023-04-03 UICollectionView 长按排序

2023-04-03 UICollectionView 长按排序

作者: cc帅气的昵称 | 来源:发表于2023-04-02 14:16 被阅读0次

https://blog.csdn.net/LIUXIAOXIAOBO/article/details/121772551

- (void)longPressAction:(UIGestureRecognizer*)gesture

{

    UIGestureRecognizerState state = gesture.state;

    CGPoint location = [gesture locationInView:self.collectionView];

    NSIndexPath  *currentPath  = [self.collectionView indexPathForItemAtPoint:location];

    static NSInteger    fromIndex = 0;

    static NSIndexPath  *oriIndexpath = nil;

    static UIView    *snapShotView = nil;

    switch (state) {

    ///手势开始

        case UIGestureRecognizerStateBegan:

        {

            ///手势开始,获取初始的IndexPath

            oriIndexpath = currentPath;

            fromIndex = currentPath.row;

            TPEditColumnCell  *oriCell = (TPEditColumnCell*)[self.collectionView cellForItemAtIndexPath:oriIndexpath];

            /* 这里创建一个对cell的截图视图,用来实现拖动过程中的

            移动效果

            */

            snapShotView = [self customSnapshoFromView:oriCell];

            snapShotView.backgroundColor = [UIColor clearColor];

            __block CGPoint center = oriCell.center;

            snapShotView.center = center;

            snapShotView.alpha = 0.0;

            [self.collectionView addSubview:snapShotView];

            [UIView animateWithDuration:0.35 animations:^{

                center.x = location.x;

                center.y = location.y;

                snapShotView.center = center;

                snapShotView.transform = CGAffineTransformMakeScale(1.1, 1.1);

                snapShotView.alpha = 0.9;

                oriCell.hidden = YES;

                oriCell.alpha = 0.0f;

            }];

            break;

        }

            ///手势变化

        case UIGestureRecognizerStateChanged:

        {

            if (!snapShotView) {

                return;

            }

            CGPoint snapShotCenter = snapShotView.center;

            snapShotCenter.x = location.x;

            snapShotCenter.y = location.y;

            /*

            手势移动的过程中,截图视图始终跟随手势移动

            */

            snapShotView.center = snapShotCenter;

            UICollectionViewCell  *currentCell = [self.collectionView cellForItemAtIndexPath:currentPath];

            UICollectionViewCell  *oriCell    = [self.collectionView cellForItemAtIndexPath:oriIndexpath];

            currentCell.alpha = 0.0f;

            oriCell.alpha = 0.0f;

            if (currentPath.row == 0 || currentPath.section == 1) {

                    oriCell.alpha= 1.0f;

                    currentCell.alpha = 1.0f;

                    break;

                }

            if (currentPath &&![currentPath isEqual:oriIndexpath]) {

                TPSectionViewModel *sectionViewModel = self.dataArray[oriIndexpath.section];

                TPColumnItemModel *model = sectionViewModel.dataArray[oriIndexpath.item];

                NSMutableArray *mArray = [sectionViewModel.dataArray mutableCopy];

                [mArray removeObject:model];

                [mArray insertObject:model atIndex:currentPath.row];

                sectionViewModel.dataArray = mArray.copy;

                // 移动真实的cell(相对于截图视图来说是真实的cell)

                [self.collectionView moveItemAtIndexPath:oriIndexpath toIndexPath:currentPath];

                oriIndexpath = currentPath;

            }

            break;

        }

          ///手势结束 

        default:

        {

            if (!snapShotView) {

                return;

            }

            UICollectionViewCell *OriCell  = [self.collectionView cellForItemAtIndexPath:oriIndexpath];

            UICollectionViewCell *currentCell = [self.collectionView cellForItemAtIndexPath:currentPath];

            [UIView animateWithDuration:0.35 animations:^{

                __block CGPoint snapShotCenter = OriCell.center;

                snapShotView.center = snapShotCenter;

                snapShotView.transform = CGAffineTransformIdentity;

                snapShotView.alpha = 0.0f;

                OriCell.hidden = NO;

                OriCell.alpha =1.0f;

                currentCell.hidden =NO;

                currentCell.alpha = 1.0f;

            } completion:^(BOOL finished) {

                [snapShotView removeFromSuperview];

                snapShotView = nil;

            }];

            ///手势结束,修改数据源

            NSMutableArray *array = [self.dataArray mutableCopy];

            TPColumnItemModel *itemModel = array[fromIndex];

            [array removeObject:itemModel];

            [array insertObject:itemModel atIndex:currentPath.item];

            self.dataArray = array;

            break;

        }

    }

}

/// 对视图截图

- (UIView *)customSnapshoFromView:(UIView *)inputView {

    UIView *snapshot = UIView.new;

    snapshot = [[UIView alloc]initWithFrame:inputView.frame];

    UIGraphicsBeginImageContextWithOptions(inputView.bounds.size, NO, 7.0);

    [inputView.layer renderInContext:UIGraphicsGetCurrentContext()];

    UIImage * viewImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    UIImageView *imageView = [[UIImageView alloc]initWithImage:viewImage];

    [snapshot addSubview:imageView];

    return snapshot;

}

相关文章

网友评论

      本文标题:2023-04-03 UICollectionView 长按排序

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