美文网首页
自定义表视图cell的拖动排序

自定义表视图cell的拖动排序

作者: 为什么划船不靠桨 | 来源:发表于2021-03-22 15:50 被阅读0次

    1.为表视图添加长按收拾

    UILongPressGestureRecognizer *longTap = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handlelongGesture:)];
    [_tableView addGestureRecognizer:longTap];
    

    2.实现排序手势

    #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;
                    
                    if (sourceIndexPath.section == indexPath.section) {
    
                        NLPublishTravelNextSectionModel *sectionModel = self.arr[sourceIndexPath.section];
                        [sectionModel.data exchangeObjectAtIndex:indexPath.row withObjectAtIndex:sourceIndexPath.row];
    
                        //[self.tableView reloadData];
                        [self.tableView moveRowAtIndexPath:sourceIndexPath toIndexPath:indexPath];
                        
                        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;
            }
        }
    }
    

    3.创建截屏实现拖拽的动画效果

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

    相关文章

      网友评论

          本文标题:自定义表视图cell的拖动排序

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