滑动即可删除行而无需点击删除按钮 [英] Swipe to delete row without having to hit the delete button

查看:31
本文介绍了滑动即可删除行而无需点击删除按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道滑动删除功能的工作原理,虽然我不太确定如何制作它,而不是让删除按钮出现删除我宁愿让它在滑动时删除单元格向左或向右移动单元格.

I know how the swipe to delete function works, though I'm not quite sure how to make it so that rather than having the delete button come up to delete I'd rather make it so that the cell is deleted upon swiping the cell to left or right.

这是我让单元格向左注册向左滑动手势的地方:

Here's where I make the cell register the left swipe gesture to the left:

    UISwipeGestureRecognizer* swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeLeft:)];
    [swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
    [cell addGestureRecognizer:swipeLeft];

这里是选择器的方法:

- (void)swipeLeft:(UIGestureRecognizer *)gestureRecognizer {
    if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
        UITableViewCell *cell = (UITableViewCell *)gestureRecognizer.view;
        NSIndexPath* indexPath = [self.tableView indexPathForCell:cell];

    }
}

现在从 UITableview 中删除单元格可以正常工作;但是,我需要一个动画来显示单元格向左移动一定距离然后删除.我没有什么删除按钮.

Now it works just fine when deleting cells from UITableview; however, I need an animation to show the cell moving to the left a certain distance and then deleting. I do not what a delete button.

这是带有动画的删除行:

edit: Here's the delete row with animation:

    [self.tableView deleteRowsAtIndexPaths:@[indexPath]
                          withRowAnimation:UITableViewRowAnimationLeft];

    [self.tableView reloadData];

这删除得很好,但我可以让它像淡入淡出动画一样创建,或者我可以修改它以便在滑动指定距离后提交删除吗?

This deletes just fine, but can I make it so that it creates like a fade animation or can I modify this so after after swiping a specified distance will commit the delete?

推荐答案

这是我为静态表格视图制作的自定义滑动,但应该适用于动态表格视图.它将允许您滑动到设置的最大 x 位置.如果用户释放它,它将动画回到原始位置.

This is a custom swipe I had made for a static table view, however should work for a dynamic table view. It will allow you to swipe to a set max x postition. If the user releases it will animate back to the original location.

我已经调整了代码,因此它应该适用于动态单元格.但尚未测试.

I've tweaked the code so it should work with a dynamic cell. But not tested yet.

添加这个本地变量

@property (nonatomic) CGFloat changeX;

只需将 pan GR 连接到情节提要并连接到此操作.

Just connect a pan GR to the storyboard and connect to this action.

- (IBAction)dragCell:(UIPanGestureRecognizer *) gestureRecognizer
{
    CGPoint swipeLocation = [gestureRecognizer locationInView:self.tableView];
    NSIndexPath *swipedIndexPath = [self.tableView indexPathForRowAtPoint:swipeLocation];
    UITableViewCell*swipedCell = [self.tableView cellForRowAtIndexPath:swipedIndexPath];

    if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {
        self.changeX = swipeLocation.x;
    }
    if (gestureRecognizer.state == UIGestureRecognizerStateChanged) {
        CGFloat maxRight = 100; // change this to increase distance of hit point
        CGFloat newX = swipeLocation.x - self.changeX; // relative from first touch location
        if (newX <= 0) newX = 0;
        if (newX >= maxRight) {
            newX = maxRight;
            NSLog(@"Hit the point where I'll allow delete");
        }
        swipedCell.frame = CGRectMake(newX, swipedCell.frame.origin.y, swipedCell.frame.size.width, swipedCell.frame.size.height);
    }

    if (gestureRecognizer.state == UIGestureRecognizerStateEnded || gestureRecognizer.state == UIGestureRecognizerStateCancelled) {

        [UIView animateWithDuration:0.5 animations:^{
            swipedCell.frame = CGRectMake(0, swipedCell.frame.origin.y, swipedCell.frame.size.width, swipedCell.frame.size.height);
        }
        completion:^(BOOL finished) {
            //
        }];
    }
}

这篇关于滑动即可删除行而无需点击删除按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆