美文网首页
iOS UITableView左滑事件

iOS UITableView左滑事件

作者: 青椒辣不辣 | 来源:发表于2021-01-05 18:40 被阅读0次

UISwipeActionsConfiguration-UIContextualAction

image.png
//左滑事件
- (nullable UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath API_AVAILABLE(ios(11.0)){
    return [UISwipeActionsConfiguration configurationWithActions:[self getActionsWithType:indexPath.row % 3]];
}

-(NSArray *)getActionsWithType:(RJActionType)type{
    NSArray *titleArrray;
    if (type==RJActionType_text) {
        titleArrray = @[@"删除",@"不显示",@"标为已读"];
    }else if (type==RJActionType_image){
        titleArrray = @[@"",@"",@""];
    }else if (type==RJActionType_textImage){
        titleArrray = @[@"删除",@"不显示",@"标为已读"];
    }
    UIContextualAction *action1 = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleDestructive title:titleArrray[0] handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
        NSLog(@"UIContextualAction-删除要实现的代码");
        completionHandler(NO);//调用次回调以后,点击以后会回到复原Cell,传YES/NO没有发现区别
    }];
    UIContextualAction *action2 = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleNormal title:titleArrray[1] handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
        NSLog(@"UIContextualAction-不显示要实现的代码");
        completionHandler(NO);
    }];
    UIContextualAction *action3 = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleNormal title:titleArrray[2] handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
        NSLog(@"UIContextualAction-标为已读要实现的代码");
        completionHandler(NO);
    }];
    
    action1.backgroundColor = [UIColor colorWithRed:227/255.0 green:90/255.0 blue:80/255.0 alpha:1];
    action2.backgroundColor = [UIColor colorWithRed:227/255.0 green:151/255.0 blue:73/255.0 alpha:1];
    action3.backgroundColor = [UIColor colorWithRed:66/255.0 green:66/255.0 blue:66/255.0 alpha:1];
    
    if (type == RJActionType_image || type == RJActionType_textImage) {
        action1.image = [UIImage imageNamed:@"rj_tableview_shanchu"];
        action2.image = [UIImage imageNamed:@"rj_tableview_buxianshi"];
        action3.image = [UIImage imageNamed:@"rj_tableview_yidu"];
    }
    return @[action1,action2,action3];
}

//UITableView -> _UITableViewCellSwipeContainerView -> UISwipeActionPullView -> UISwipeActionStandardButton(UIView + UIButtonLabel)
- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath {
    for (UIView * subContainerView in tableView.subviews) {
        if ([subContainerView isKindOfClass:NSClassFromString(@"_UITableViewCellSwipeContainerView")]) {
            for (UIView * pullView in subContainerView.subviews) {
                if ([pullView isKindOfClass:NSClassFromString(@"UISwipeActionPullView")]) {
                    for (UIView *standardButton in pullView.subviews) {
                        if ([standardButton isKindOfClass:NSClassFromString(@"UISwipeActionStandardButton")]) {
                            for (UIView *endView in standardButton.subviews) {
                                if ([endView isKindOfClass:NSClassFromString(@"UIImageView")]) {
                                    NSLog(@"UIImageView-%@",endView);
                                    continue;
                                }
                                if ([endView isKindOfClass:NSClassFromString(@"UIButtonLabel")]) {
                                    NSLog(@"UIButtonLabel-%@",endView);
                                    continue;
                                }
                                if ([endView isKindOfClass:NSClassFromString(@"UIView")]) {
                                    NSLog(@"UIView-%@",endView);
                                    endView.userInteractionEnabled = YES;
                                    continue;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
左滑后的层级关系
左滑前的层级关系
左滑后的层级关系
左滑后的展开层级关系
由图可以看出:
  • 左滑以前(RJListTableViewCell *)之间是并列的
  • 左滑以后目标(RJListTableViewCell *)(_UITableViewCellSwipeContainerView *)包装了起来,而此时被(_UITableViewCellSwipeContainerView *)包装的还有(UISwipeActionPullView *),而此时(_UITableViewCellSwipeContainerView *)与正常的cell之间是并列的
层级关系: UITableView -> _UITableViewCellSwipeContainerView -> UISwipeActionPullView -> UISwipeActionStandardButton(UIView + UIButtonLabel)

((UITableView *)0x7fee29831e00)
((_UITableViewCellSwipeContainerView *)0x7fee25c23c70)
((UISwipeActionPullView *)0x7fee25c056b0)
((UISwipeActionStandardButton *)0x7fee25c1f550)
((UIView *)0x7fee25c1b680)
((UIButtonLabel *)0x7fee25c2b5e0)

相关文章

网友评论

      本文标题:iOS UITableView左滑事件

      本文链接:https://www.haomeiwen.com/subject/viywoktx.html