iOS自定义左右滑动事件(适配iOS11)

iOS11中tableview的左右滑动事件,iOS更新了左右滑动事件,新增添了两个方法,可以为事件自定义添加图片和标题。不过有点限制,就是图片和标题都会变成白色,不能显示原图和设置标题大小和颜色。效果如下


WechatIMG36.jpeg

WechatIMG37.jpeg

进入tableview的delegate中我们可以看到增加了两个代理方法

// Swipe actions
// These methods supersede -editActionsForRowAtIndexPath: if implemented
// return nil to get the default swipe actions
- (nullable UISwipeActionsConfiguration *)tableView:(UITableView *)tableView leadingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath API_AVAILABLE(ios(11.0)) API_UNAVAILABLE(tvos);
- (nullable UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath API_AVAILABLE(ios(11.0)) API_UNAVAILABLE(tvos);

一个是左划代理事件,另一个是右划代理事件
同时还增加了两个类,其中UIContextualAction是事件,UISwipeActionsConfiguration是滑动事件设置。是代理中的返回值。

UISwipeActionsConfiguration
UIContextualAction
  • UIContextualAction 事件的类比较简单,可以看到只有一个类的实例化方法,一个style设置类型,一个title,backgroundcolor,一个image就没有了。


    WechatIMG38.jpeg
  • UISwipeActionsConfiguration 就更简单了,一个类实例化方法加一个全滑动事件,performsFirstActionWithFullSwipe设置cell全划的时候自动响应活动事件,默认是true,自动响应。


    391510544061_.pic_hd.jpg

最后看看实现的代码
1.右划

#ifdef __IPHONE_11_0
- (nullable UISwipeActionsConfiguration *)tableView:(UITableView *)tableView leadingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (@available(iOS 11.0, *)) {
        UIContextualAction *deleteRowAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleNormal title:@"删除" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
              //响应事件在这里操作
        }];
        //设置图片,但是设置不了原图,都是被默认为白色了,字体也是
        UIImage *image =  [[UIImage imageNamed:@"ico_delete"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        [deleteRowAction setImage:image];
        deleteRowAction.backgroundColor = [UIColor redColor];

        UIContextualAction *editRowAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleNormal title:@"编辑" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
              //响应事件在这里操作
        }];
        editRowAction.image = [UIImage imageNamed:@"ico_edit"];
        editRowAction.backgroundColor = [UIColor blueColor];
        UISwipeActionsConfiguration *config = [UISwipeActionsConfiguration configurationWithActions:@[deleteRowAction,editRowAction]];
        //设置全屏滑动时不自定响应事件
        config.performsFirstActionWithFullSwipe = false;
        return config;
    }else{
        return nil;
    }
}

2.左划

- ( UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath {

    if (@available(iOS 11.0, *)) {
        UIContextualAction *deleteRowAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleNormal title:@"删除" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
        }];
        UIImage *image =  [[UIImage imageNamed:@"ico_delete"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        [deleteRowAction setImage:image];
        deleteRowAction.backgroundColor = [UIColor redColor];
        UIContextualAction *editRowAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleNormal title:@"编辑" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
        }];
        editRowAction.image = [UIImage imageNamed:@"ico_edit"];
        editRowAction.backgroundColor = [UIColor blueColor];

        UISwipeActionsConfiguration *config = [UISwipeActionsConfiguration configurationWithActions:@[deleteRowAction,editRowAction]];
        config.performsFirstActionWithFullSwipe = false;
        return config;
    }else{
        return nil;
    }
}

PS

那么我们想有没有方法改变图标的颜色的字体颜色大小呢。

用runtime获取一下UIContextualAction的所有属性看看有哪些。

        unsigned int count = 0;
        Ivar *list = class_copyIvarList([UIContextualAction class], &count1);
        for (int i = 0; i < count; i ++) {
            Ivar item = list[I];
            NSString *name = [NSString stringWithUTF8String:ivar_getName(item)];
            NSString *type = [NSString stringWithUTF8String:ivar_getTypeEncoding(item)];
            NSLog(@"name = %@  type = %@",name,type);
        }
401510553216_.pic_hd.jpg

明显的没有我们想要的UIImageView和UILabel,说明UIContextualAction并没有直接持有他们作为属性。用KVC的方法获取不到,自然也就改不了。

iOS11之前的左右划事件

iOS11之前默认的划动事件是不能直接设置图标的,只能设置标题和背景颜色。先看看效果


421510554670_.pic_hd.jpg

实现一样很简单,实现delegate中的方法即可,如下

- (nullable NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewRowAction *delete = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
       //响应事件在这里操作
    }];
    
    delete.backgroundColor = [UIColor redColor];
    UITableViewRowAction *edit = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"编辑" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
       //响应事件在这里操作
    }];
    edit.backgroundColor = [UIColor blueColor];
    return @[delete,edit];
}

但是有时候我们需要自定义添加事件的图标和改字体的大小和颜色呢?如以下效果
451510555768_.pic_hd.jpg

我们看看事件的响应UITableViewRowAction里面的属性有什么,同样的用runtime获取一遍所有的属性

 unsigned int count = 0;
         Ivar *list = class_copyIvarList([UITableViewRowAction class], &count);
         for (int i = 0; i < count; i ++) {
         Ivar item = list[I];
         NSString *name = [NSString stringWithUTF8String:ivar_getName(item)];
         NSString *type = [NSString stringWithUTF8String:ivar_getTypeEncoding(item)];
         NSLog(@"name = %@  type = %@",name,type);
         }

461510555875_.pic_hd.jpg

从log中我们可以看到,在事件UITableViewRowAction中有个button的属性,类型是UITableViewCellActionButton类型。这就是我们所需要的做文章的view了。经过探究,这个button是在实例化UITableViewRowAction之后内部实例化的,我们用一个KVC获取到该button,然后自定一个我们想要的效果,贴在该button上面即可,注意的是:获取该button时不能直接获取,需要在事件UITableViewRowAction实例化后延时零点几秒之后才能获取得到

实现代码如下

- (nullable NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewRowAction *delete = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
    }];
    delete.backgroundColor = [UIColor grayColor];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        /*
         unsigned int count = 0;
         Ivar *list = class_copyIvarList([UITableViewRowAction class], &count);
         for (int i = 0; i < count; i ++) {
         Ivar item = list[i];
         NSString *name = [NSString stringWithUTF8String:ivar_getName(item)];
         NSString *type = [NSString stringWithUTF8String:ivar_getTypeEncoding(item)];
         NSLog(@"name = %@  type = %@",name,type);
         }*/
        //用KVC获取该button
        UIControl *baseView = [delete valueForKey:@"_button"];
        //自定义一个view加作为subview加在button上面即可实现
        UIView *custom = [[UIView alloc] initWithFrame:baseView.bounds];
        custom.backgroundColor = [UIColor lightGrayColor];
        custom.userInteractionEnabled = NO;
        UIImageView *icon = [[UIImageView alloc] initWithFrame:CGRectMake((custom.frame.size.width - 30)/2, custom.frame.size.height/2 - 30, 30, 30)];
        icon.image = [UIImage imageNamed:@"ico_delete"];
        icon.contentMode = UIViewContentModeCenter;
        [custom addSubview:icon];
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, custom.frame.size.height/2 + 5, custom.frame.size.width, 20)];
        label.text = @"删除";
        label.font = [UIFont systemFontOfSize:14];
        label.textAlignment = NSTextAlignmentCenter;
        [custom addSubview:label];
        [baseView addSubview:custom];
    });
    UITableViewRowAction *edit = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"编辑" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
    }];
    edit.backgroundColor = [UIColor grayColor];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        UIControl *baseView = [edit valueForKey:@"_button"];
        UIView *custom = [[UIView alloc] initWithFrame:baseView.bounds];
        custom.backgroundColor = [UIColor lightGrayColor];
        custom.userInteractionEnabled = NO;
        UIImageView *icon = [[UIImageView alloc] initWithFrame:CGRectMake((custom.frame.size.width - 30)/2, custom.frame.size.height/2 - 30, 30, 30)];
        icon.image = [UIImage imageNamed:@"ico_edit"];
        icon.contentMode = UIViewContentModeCenter;
        [custom addSubview:icon];
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, custom.frame.size.height/2 + 5, custom.frame.size.width, 20)];
        label.text = @"编辑";
        label.font = [UIFont systemFontOfSize:14];
        label.textAlignment = NSTextAlignmentCenter;
        [custom addSubview:label];
        [baseView addSubview:custom];
    });
    return @[delete,edit];
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,470评论 6 501
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,393评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,577评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,176评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,189评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,155评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,041评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,903评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,319评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,539评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,703评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,417评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,013评论 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,664评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,818评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,711评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,601评论 2 353

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,651评论 18 139
  • 国家电网公司企业标准(Q/GDW)- 面向对象的用电信息数据交换协议 - 报批稿:20170802 前言: 排版 ...
    庭说阅读 10,958评论 6 13
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 172,074评论 25 707
  • iOS 11 为整个生态系统的 UI 元素带来了一种更加大胆、动态的新风格。 本文介绍iOS11中在UI方面做了哪...
    阿凡提说AI阅读 590评论 0 1
  • 还记得小时候学习书法吗?描红了一遍又一遍,再对照着写一遍又一遍,学习素描也是先对照着参照物画,对着画一次又一次,再...
    遇上缘阅读 168评论 0 1