美文网首页
TableView长按拖动Cell改变位置

TableView长按拖动Cell改变位置

作者: 这位网友 | 来源:发表于2019-05-09 10:43 被阅读0次

    需要实现简单的cell拖动效果
    首先是在你的TableView上添加长按手势,然后实现长按手势就可以了

    一、添加长按手势
    UILongPressGestureRecognizer *longTap = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handlelongGesture:)];
    [_tableView addGestureRecognizer:longTap];
    
    二、实现长按拖动效果
    #pragma mark - Cell拖动排序
    - (void)handlelongGesture:(UILongPressGestureRecognizer *)sender
    {
        UILongPressGestureRecognizer *longPress = (UILongPressGestureRecognizer *)sender;
        UIGestureRecognizerState state = longPress.state;
        
        CGPoint location = [sender locationInView:self.tableView];
        NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
        
        static UIView *snapshot = nil;
        static NSIndexPath *sourceIndexPath = nil;
        
        switch (state) {
            case UIGestureRecognizerStateBegan: {
                if (indexPath) {
                    sourceIndexPath = indexPath;
                    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
                    
                    // Take a snapshot of the selected row using helper method.
                    snapshot = [self customSnapshoFromView:cell];
                    
                    // Add the snapshot as subview, centered at cell's center...
                    __block CGPoint center = cell.center;
                    snapshot.center = center;
                    snapshot.alpha = 0.0;
                    [self.tableView addSubview:snapshot];
                    [UIView animateWithDuration:0.25 animations:^{
                        // Offset for gesture location.
                        center.y = location.y;
                        snapshot.center = center;
                        snapshot.transform = CGAffineTransformMakeScale(1.05, 1.05);
                        snapshot.alpha = 0.98;
                        
                        cell.alpha = 0.0f;
                        
                    } completion:^(BOOL finished) {
                        cell.hidden = YES;
                    }];
                }
                break;
            }
                
            case UIGestureRecognizerStateChanged: {
                CGPoint center = snapshot.center;
                center.y = location.y;
                snapshot.center = center;
                // Is destination valid and is it different from source?
                if (indexPath && ![indexPath isEqual:sourceIndexPath]) {
                    // ... update data source.
                    // 这里需要注意,我是以section为单位写的
                    [self.dateList exchangeObjectAtIndex:indexPath.section withObjectAtIndex:sourceIndexPath.section];
                    // ... move the rows.
                    // 你要看你是拖动section还是拖动row了[]([]())
                    // [self.tableView moveRowAtIndexPath:sourceIndexPath toIndexPath:indexPath];
                    [self.tableView moveSection:sourceIndexPath.section toSection:indexPath.section];
                    // ... and update source so it is in sync with UI changes.
                    sourceIndexPath = indexPath;
                }
                break;
            }
            
            default: {
                // Clean up.
                UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:sourceIndexPath];
                [UIView animateWithDuration:0.25 animations:^{
                    snapshot.center = cell.center;
                    snapshot.transform = CGAffineTransformIdentity;
                    snapshot.alpha = 0.0;
                    cell.alpha = 1.0f;
                } completion:^(BOOL finished) {
                    cell.hidden = NO;
                    [snapshot removeFromSuperview];
                    snapshot = nil;
                }];
                sourceIndexPath = nil;
                break;
            }
        }
    }
    
    - (UIView *)customSnapshoFromView:(UIView *)inputView
    {
        UIView *snapshot = nil;
        if ([[[UIDevice currentDevice] systemVersion] doubleValue] < 7.0) {
            //ios7.0 以下通过截图形式保存快照
            snapshot = [self customSnapShortFromViewEx:inputView];
        }else{
            //ios7.0 系统的快照方法
            snapshot = [inputView snapshotViewAfterScreenUpdates:YES];
        }
        
        snapshot.layer.masksToBounds = NO;
        snapshot.layer.cornerRadius = 0.0;
        snapshot.layer.shadowOffset = CGSizeMake(-5.0, 0.0);
        snapshot.layer.shadowRadius = 5.0;
        snapshot.layer.shadowOpacity = 0.4;
        return snapshot;
    }
    
    - (UIView *)customSnapShortFromViewEx:(UIView *)inputView
    {
        CGSize inSize = inputView.bounds.size;
        // 下面方法,第一个参数表示区域大小。第二个参数表示是否是非透明的。如果需要显示半透明效果,需要传NO,否则传YES。第三个参数就是屏幕密度了
        UIGraphicsBeginImageContextWithOptions(inSize, NO, [UIScreen mainScreen].scale);
        [inputView.layer renderInContext:UIGraphicsGetCurrentContext()];
        UIImage *image= UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        UIImageView* snapshot = [[UIImageView alloc] initWithImage:image];
        
        return snapshot;
    }
    

    参考自:iOS-TableView拖动Cell更换次序

    相关文章

      网友评论

          本文标题:TableView长按拖动Cell改变位置

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