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


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值