很常iOS常用iOS进阶

iOS UITableView拖动排序

2020-12-03  本文已影响0人  最强的小强

前言:
排序思路:拖拽排序的主要思路是利用在UITableView上添加一个长按的手势UILongPressGestureRecognizer实现的。 通过监听手势拖拽开始,拖拽中,拖拽结束的事件,然后根据不同的状态做相应的操作。

一、在UITableView上添加一个长按的手势
// 添加长按手势
 UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressRecognizer:)]; 
 longPress.minimumPressDuration = 0.3;
[self.tableView addGestureRecognizer:longPress];
二、监听排序过程,实现UI交互逻辑
// cell长按拖动排序
- (void)longPressRecognizer:(UILongPressGestureRecognizer *)longPress{
    //获取长按的点及cell
    CGPoint location = [longPress locationInView:self.tableView];
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
    UIGestureRecognizerState state = longPress.state;
    static UIView *snapView = nil;
    static NSIndexPath *sourceIndex = nil;
    switch (state) {
        case UIGestureRecognizerStateBegan:{
            if (indexPath) {
                sourceIndex = indexPath;
                WENewMonitorCompanyCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
                snapView = [self customViewWithTargetView:cell];
                __block CGPoint center = cell.center;
                snapView.center = center;
                snapView.alpha = 0.0;
                [self.tableView addSubview:snapView];
                [UIView animateWithDuration:0.1 animations:^{
                    center.y = location.y;
                    snapView.center = center;
                    snapView.transform = CGAffineTransformMakeScale(1.05, 1.05);
                    snapView.alpha = 0.5;
                    cell.alpha = 0.0;
                }];
            }
        }
        break;
        case UIGestureRecognizerStateChanged:{
            CGPoint center = snapView.center;
            center.y = location.y;
            snapView.center = center;
            WENewMonitorCompanyCell *cell = [self.tableView cellForRowAtIndexPath:sourceIndex];
            cell.alpha = 0.0;
            if (indexPath && ![indexPath isEqual:sourceIndex]) {
                [self.companyArray exchangeObjectAtIndex:indexPath.row withObjectAtIndex:sourceIndex.row];
                [self.tableView moveRowAtIndexPath:sourceIndex toIndexPath:indexPath];
                sourceIndex = indexPath;
            }
        }
        break;
        default:{
            WENewMonitorCompanyCell *cell = [self.tableView cellForRowAtIndexPath:sourceIndex];
            [UIView animateWithDuration:0.25 animations:^{
                snapView.center = cell.center;
                snapView.transform = CGAffineTransformIdentity;
                snapView.alpha = 0.0;
                cell.alpha = 1.0;
            } completion:^(BOOL finished) {
                [snapView removeFromSuperview];
                snapView = nil;
            }];
            sourceIndex = nil;
            [self userSortMontiorCompany];
        }
        break;
    }
}

//截取选中cell
- (UIView *)customViewWithTargetView:(UIView *)target{
    UIGraphicsBeginImageContextWithOptions(target.bounds.size, NO, 0);
    [target.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    UIView *snapshot = [[UIImageView alloc] initWithImage:image];
    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;
}

三、最后在拖拽结束的手势里,调用接口进行数据排序就可以了
#pragma mark - 监控企业排序
- (void)userSortMontiorCompany {
    NSString *path = [NSString stringWithFormat:@"label/cfcp/monitor/monitorOrder"];
    NSMutableArray *params = [[NSMutableArray alloc] init];
    for (int i = 0; i < self.companyArray.count; i++) {
        WENewMonitorCompanyModel *model = self.companyArray[i];
        NSDictionary *dict = @{@"id":model.ID,
                               @"ruleType":model.ruleType,
                               @"orderNo":@(i+1)
                                };
        [params addObject:dict];
    }
    NSDictionary *parameters = @{@"order":params};
    [WEHttpRequest dataTaskWithConfig:^(WEUrlRequestConfig * _Nonnull request) {
        request.methodType = WEHttpMethodPOST;
        request.baseUrl = HostUrl;
        request.url = path;
        request.parameters = parameters;
        [request setValue:[WELoginManager sharedInstance].accessToken forHeaderField:kAccessToken];
        [request setValue:@"application/json" forHeaderField:@"Content-Type"];
    } success:^(NSDictionary * _Nullable responseObject) {

    } failed:^(NSError * _Nullable error) {

    }];
}
上一篇下一篇

猜你喜欢

热点阅读