美文网首页iOS开发
iOS自定义左右滑动事件(适配iOS11)

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

作者: yuandiLiao | 来源:发表于2017-09-22 17:51 被阅读0次

    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];
    }
    
    

    相关文章

      网友评论

        本文标题:iOS自定义左右滑动事件(适配iOS11)

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