长按手势删除TableviewCell

先拜叶神,原文地址

基本思路

当识别到press 手势的时候对cell原有的view 截图获得snapview,并隐藏原有view,动画都设置在snapview上面。long Press位置移动时,snapview的position随着移动,如果long Press手势结束时,其位置在距离cell边缘的一定范围内,则删除此cell,否则,删除snapview,原有view hidden = NO,恢复原样。

在cell中添加long press guestur recognizer
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPressGuesture:)];
[self.contentView addGestureRecognizer:longPress];

guesture recognizer的方法

- (void)handleLongPressGuesture:(UILongPressGestureRecognizer *)guesture {
    CGPoint startPoint;
    
    switch (guesture.state) {
//手势开始
        case UIGestureRecognizerStateBegan:
        {
            NSLog(@"longpress started");
//获得截图
            snapView = [_containerView snapshotViewAfterScreenUpdates:NO];
//叶神原文中重设了锚点,我没有
//            snapView.layer.anchorPoint = CGPointMake(startPoint.x/snapView.layer.frame.size.width-0.1, startPoint.y/snapView.layer.frame.size.height-0.1);
            snapView.frame = _containerView.frame;
            snapView.transform = CGAffineTransformMakeRotation(M_PI/6);
            startPoint = [guesture locationInView:self.contentView];

            [self.contentView addSubview:snapView];
//隐藏原有view
            _shadowView.hidden = YES;
            _containerView.hidden = YES;
            
            
            break;
        }
//手势移动
        case UIGestureRecognizerStateChanged: {
            NSLog(@"longpress changed");
            CGPoint changedPoint = [guesture locationInView:self.contentView];
//一开始把snapView设为了局部变量,这个动画不管用,将snapView改为全局变量就可以了。为啥嗯?
            [UIView animateWithDuration:0.5 animations:^{
                snapView.layer.position = changedPoint;
            }];
            break;
        }
//手势结束
        case UIGestureRecognizerStateEnded: {
            NSLog(@"longpress ended");
            CGPoint endedPoint = [guesture locationInView:self.contentView];
//设定删除边界,超过则删除
            if (endedPoint.x > self.contentView.bounds.size.width - 50 ) {
                if (_cellBlock) {
                    _cellBlock(YES, _cellIndex);

                }
            }
            else {
                if (_cellBlock) {
                    _cellBlock(NO, _cellIndex);
                    
                }
            }
//手势结束后收拾cell view,原先隐藏的view再显示,snapview移除,此处无论是否删除cell,都会移除snapview并显示原先的view。
            [snapView removeFromSuperview];
            _containerView.hidden = NO;
            _shadowView.hidden = NO;
        }
        default:
            break;
    }
删除cell的回调函数

在tableView的 cell for row at indexpath 代理方法中实现,如果确认删除,则处理datasource并执行delete row at index path 方法。

cell.cellBlock = ^(BOOL isDelete, NSIndexPath *index){
        if (isDelete) {
            
            [_allCityData removeObjectAtIndex:index.row];
            [_cityList removeObjectAtIndex:index.row];
            [self saveToNSUserDefault];
//deleteRowAtIndexPath, withRowAnimation 此方法决定删除cell的动画样式
            [self.tableView deleteRowsAtIndexPaths:@[index] withRowAnimation:UITableViewRowAnimationRight];
//删除内容后reload data;
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self.tableView reloadData];});
        }

    };

你可能感兴趣的:(长按手势删除TableviewCell)