tableviewcell拖拽移动

action1:UILongPressGestureRecognizer

- (void)longPressRecognizer:(UILongPressGestureRecognizer *)longPress

{

    WS(weakSelf);

    CGPointlocation = [longPresslocationInView:self.tableView];

    NSIndexPath*indexPath = [self.tableViewindexPathForRowAtPoint:location];

    UIGestureRecognizerStatestate = longPress.state;

    staticUIView*snapView =nil;

    staticNSIndexPath*sourceIndex =nil;

    switch(state) {

        case UIGestureRecognizerStateBegan:

        {

                if(indexPath) {

                    sourceIndex = indexPath;

                    UITableViewCell*cell = [self.tableViewcellForRowAtIndexPath:indexPath];

                    cell.selectionStyle = UITableViewCellSelectionStyleNone;

                    snapView = [selfcustomViewWithTargetView:cell];

                    __blockCGPointcenter = cell.center;

                    snapView.center= center;

                    snapView.alpha=0.0;

                    [self.tableViewaddSubview: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:

        {

                CGPointcenter = snapView.center;

                center.y= location.y;

                snapView.center= center;

                UITableViewCell*cell = [self.tableViewcellForRowAtIndexPath:sourceIndex];

                cell.alpha=0.0;

                if(indexPath && ![indexPathisEqual:sourceIndex]) {

                [self.addSignCountMArrayexchangeObjectAtIndex:indexPath.rowwithObjectAtIndex:sourceIndex.row];

                    [self.tableViewmoveRowAtIndexPath:sourceIndextoIndexPath:indexPath];

                    sourceIndex = indexPath;


                }


        }

        break;


        default:

        {

            UITableViewCell*cell = [self.tableViewcellForRowAtIndexPath:sourceIndex];

            [UIView animateWithDuration:0.25 animations:^{

                snapView.center= cell.center;

                snapView.transform = CGAffineTransformIdentity;

                snapView.alpha=0.0;

                cell.alpha=1.0;

                }completion:^(BOOLfinished) {

                    [snapViewremoveFromSuperview];

                    snapView =nil;

            }];

            sourceIndex =nil;

        }

        break;

    }

}

- (UIView *)customViewWithTargetView:(UIView *)target

{

    UIGraphicsBeginImageContextWithOptions(target.bounds.size,NO,0);

    [target.layer renderInContext:UIGraphicsGetCurrentContext()];

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    UIView*snapshot = [[UIImageViewalloc]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;

    returnsnapshot;

}

action2:system

[self.tableView setEditing:YES animated:YES];

/**

 Cell是否可以移动

 如返回NO,cell将不具备被拖动功能

 */

- (BOOL)tableView:(UITableView*)tableViewcanMoveRowAtIndexPath:(NSIndexPath*)indexPath{

    return YES;

}

// 这个回调实现了以后,就会出现更换位置的按钮,回调本身用来处理更换位置后的数据交换。

- (void)tableView:(UITableView*)tableViewmoveRowAtIndexPath:(NSIndexPath*)sourceIndexPathtoIndexPath:(NSIndexPath*)destinationIndexPath{

    NSLog(@"moveRowAtIndexPath---source:%@---destination:%@", sourceIndexPath, destinationIndexPath);

    // cell移动,改变数据模型

    if(sourceIndexPath.row> destinationIndexPath.row) {


        [self.dataArrayinsertObject:self.dataArray[sourceIndexPath.row]atIndex:destinationIndexPath.row];

        [self.dataArrayremoveObjectAtIndex:sourceIndexPath.row+1];

    }else{


        [self.dataArrayinsertObject:self.dataArray[sourceIndexPath.row]atIndex:destinationIndexPath.row+1];

        [self.dataArrayremoveObjectAtIndex:sourceIndexPath.row];

    }

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容