iOS 添加长按手势实现 拖动频道修改排序功能

效果

请添加图片描述

实现代码

添加手势

    
    UILongPressGestureRecognizer * longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressAction:)];
    [self.view addGestureRecognizer:longPress];

手势响应

- (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;
}


Guess you like

Origin blog.csdn.net/LIUXIAOXIAOBO/article/details/121772551
ios