You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

UITableViewCell左滑1/3距离自动触发leadingSwipeAction删除动作咨询

实现方案

系统原生UISwipeActionsConfiguration仅支持满滑触发首动作,不支持自定义触发阈值,因此需要给UITableView添加自定义平移手势,监听滑动距离,达到阈值后主动触发删除逻辑,同时保留原生左滑的视觉效果。

步骤1:添加手势配置

首先在控制器中新增属性、添加手势并遵守手势代理:

@interface YourViewController () <UIGestureRecognizerDelegate>
@property (nonatomic, assign) CGFloat swipeThreshold; // 触发阈值
@property (nonatomic, assign) BOOL hasTriggeredDelete; // 防止滑动过程重复触发
@end

在viewDidLoad中初始化配置:

- (void)viewDidLoad {
    [super viewDidLoad];
    // 阈值设置为屏幕宽度的1/3,可按需调整
    self.swipeThreshold = [UIScreen mainScreen].bounds.size.width / 3;
    self.hasTriggeredDelete = NO;
    
    // 添加平移手势
    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipePan:)];
    panGesture.delegate = self;
    [self.tableView addGestureRecognizer:panGesture];
}

// 允许自定义手势和系统原生滑动手势共存
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    return YES;
}

步骤2:实现手势监听逻辑

监听滑动偏移量,达到阈值后触发删除动作:

- (void)handleSwipePan:(UIPanGestureRecognizer *)pan {
    CGPoint translation = [pan translationInView:self.tableView];
    
    // 手势开始时重置触发标记
    if (pan.state == UIGestureRecognizerStateBegan) {
        self.hasTriggeredDelete = NO;
    }
    
    // 若你的leading动作是右滑触发,将下方translation.x < 0改为translation.x > 0即可
    if (translation.x < 0 && !self.hasTriggeredDelete) {
        CGFloat swipeDistance = fabs(translation.x);
        // 滑动距离达到阈值
        if (swipeDistance >= self.swipeThreshold) {
            self.hasTriggeredDelete = YES;
            // 获取当前滑动对应的cell行号
            CGPoint touchPoint = [pan locationInView:self.tableView];
            NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:touchPoint];
            if (indexPath) {
                // 调用删除逻辑
                [self handleDeleteActionAtIndexPath:indexPath];
                // 手动收起滑动按钮,恢复cell正常状态
                [self.tableView setEditing:NO animated:YES];
            }
        }
    }
    
    // 手势结束/取消时重置标记
    if (pan.state == UIGestureRecognizerStateEnded || pan.state == UIGestureRecognizerStateCancelled) {
        self.hasTriggeredDelete = NO;
    }
}

步骤3:抽离公共删除逻辑

把删除逻辑抽成独立方法,同时适配手势触发和按钮点击触发两种场景:

// 公共删除逻辑
- (void)handleDeleteActionAtIndexPath:(NSIndexPath *)indexPath {
    // 你原来写在UIContextualAction的handler中的删除逻辑放在这里
    // 比如更新数据源、删除tableView对应行等操作
}

// 调整原有的滑动动作配置
- (UISwipeActionsConfiguration *)tableView:(UITableView *)tableView leadingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath
{
     UIContextualAction *delete = [UIContextualAction 
contextualActionWithStyle:UIContextualActionStyleNormal title:nil 
handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull 
sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
        [self handleDeleteActionAtIndexPath:indexPath];
        completionHandler(YES);
    }];
    delete.image  = [UIImage systemImageNamed:@"trash"];

    UISwipeActionsConfiguration *actions = [UISwipeActionsConfiguration configurationWithActions:@[delete]];
    actions.performsFirstActionWithFullSwipe = NO;
    return actions;
}

内容的提问来源于stack exchange,提问作者ekashking

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.13 21:09:02