美文网首页
iOS11 SwipeAction

iOS11 SwipeAction

作者: 远辰jt | 来源:发表于2017-11-29 21:51 被阅读16次
    1. iOS11之前,UITableView有相应的方法可以给指定cell添加action。效果类似于QQ会话列表的滑动删除,置顶。实现代码
    //  iOS8之后的方法,即将过期可以使用iOS11新增的方法-tableView:trailingSwipeActionsConfigurationForRowAtIndexPath: 代替.
    // 如果实现了该方法并且返回值不是nil,会替代 -tableView:titleForDeleteConfirmationButtonForRowAtIndexPath: 的实现
    - (nullable NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
    
        UITableViewRowAction *action = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
            [self.array removeObjectAtIndex:indexPath.row];
            [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
        }];
    
        UITableViewRowAction *action2 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@" 标记为未读 " handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
    
        }];
        action2.backgroundColor = [UIColor orangeColor];
        return @[action,action2];
    }
    

    效果图


    效果图.png
    1. iOS11之后新增了更强大的方法取代上述的方法,苹果也建议使用新的方法,新的API不仅可以添加右划的action,也可以添加左划的action
    • 左扫API
    - (UISwipeActionsConfiguration *)tableView:(UITableView *)tableView leadingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath;
    
    • 右扫API
    - (UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath;
    
    1. 示例代码
    - (UISwipeActionsConfiguration *)tableView:(UITableView *)tableView leadingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath{
        UIContextualAction *action = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleDestructive title:@"删除" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
            [self.array removeObjectAtIndex:indexPath.row];
            [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationBottom];
            completionHandler(YES);
        }];
    
        UIContextualAction *action2 = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleNormal title:@"标为已读" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
            completionHandler(YES);
        }];
        action2.backgroundColor = [UIColor orangeColor];
    
        UIContextualAction *action3 = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleNormal title:@"置顶" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
            completionHandler(YES);
        }];
    
        UISwipeActionsConfiguration *config = [UISwipeActionsConfiguration configurationWithActions:@[action,action2,action3]];
        config.performsFirstActionWithFullSwipe = YES; // 默认值YES,控制第一个action是否支持全屏横扫(当滑动超过一定距离后会触发第一个action)
        return config;
    }
    
    leading.png
    1. 注意点
    • 对于SwipeAction的触发方法回
      调中有一个completionHandler的闭包,表示处理完成的回调,应该在方法完成后调用这个闭包表示处理完成,否则会有页面样式的错误, 应该删除的cell没有消失。
    错误.png
    • 默认情况下加入到UISwipeActionsConfiguration中的第一个action会有全屏手势的功能,也就是当滑动出来菜单后继续滑动会触发第一个action,如果不想要这个功能可以设置属性performsFirstActionWithFullSwipe为NO即可
    UISwipeActionsConfiguration *config = [UISwipeActionsConfiguration configurationWithActions:@[action,action2,action3]];
    config.performsFirstActionWithFullSwipe = NO;
    

    相关文章

      网友评论

          本文标题:iOS11 SwipeAction

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