iOS自定義左右滑動(dòng)事件(適配iOS11)

iOS11中tableview的左右滑動(dòng)事件葬馋,iOS更新了左右滑動(dòng)事件,新增添了兩個(gè)方法孕暇,可以為事件自定義添加圖片和標(biāo)題。不過有點(diǎn)限制聋袋,就是圖片和標(biāo)題都會(huì)變成白色,不能顯示原圖和設(shè)置標(biāo)題大小和顏色穴吹。效果如下


WechatIMG36.jpeg

WechatIMG37.jpeg

進(jìn)入tableview的delegate中我們可以看到增加了兩個(gè)代理方法

// 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);

一個(gè)是左劃代理事件幽勒,另一個(gè)是右劃代理事件
同時(shí)還增加了兩個(gè)類,其中UIContextualAction是事件港令,UISwipeActionsConfiguration是滑動(dòng)事件設(shè)置啥容。是代理中的返回值锈颗。

UISwipeActionsConfiguration
UIContextualAction
  • UIContextualAction 事件的類比較簡(jiǎn)單,可以看到只有一個(gè)類的實(shí)例化方法,一個(gè)style設(shè)置類型咪惠,一個(gè)title击吱,backgroundcolor,一個(gè)image就沒有了遥昧。


    WechatIMG38.jpeg
  • UISwipeActionsConfiguration 就更簡(jiǎn)單了,一個(gè)類實(shí)例化方法加一個(gè)全滑動(dòng)事件覆醇,performsFirstActionWithFullSwipe設(shè)置cell全劃的時(shí)候自動(dòng)響應(yīng)活動(dòng)事件,默認(rèn)是true炭臭,自動(dòng)響應(yīng)永脓。


    391510544061_.pic_hd.jpg

最后看看實(shí)現(xiàn)的代碼
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)) {
              //響應(yīng)事件在這里操作
        }];
        //設(shè)置圖片,但是設(shè)置不了原圖鞋仍,都是被默認(rèn)為白色了常摧,字體也是
        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)) {
              //響應(yīng)事件在這里操作
        }];
        editRowAction.image = [UIImage imageNamed:@"ico_edit"];
        editRowAction.backgroundColor = [UIColor blueColor];
        UISwipeActionsConfiguration *config = [UISwipeActionsConfiguration configurationWithActions:@[deleteRowAction,editRowAction]];
        //設(shè)置全屏滑動(dòng)時(shí)不自定響應(yīng)事件
        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

那么我們想有沒有方法改變圖標(biāo)的顏色的字體顏色大小呢。

用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之前默認(rèn)的劃動(dòng)事件是不能直接設(shè)置圖標(biāo)的,只能設(shè)置標(biāo)題和背景顏色详炬。先看看效果


421510554670_.pic_hd.jpg

實(shí)現(xiàn)一樣很簡(jiǎn)單盐类,實(shí)現(xiàn)delegate中的方法即可寞奸,如下

- (nullable NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewRowAction *delete = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"刪除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
       //響應(yīng)事件在這里操作
    }];
    
    delete.backgroundColor = [UIColor redColor];
    UITableViewRowAction *edit = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"編輯" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
       //響應(yīng)事件在這里操作
    }];
    edit.backgroundColor = [UIColor blueColor];
    return @[delete,edit];
}

但是有時(shí)候我們需要自定義添加事件的圖標(biāo)和改字體的大小和顏色呢呛谜?如以下效果
451510555768_.pic_hd.jpg

我們看看事件的響應(yīng)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中有個(gè)button的屬性隐岛,類型是UITableViewCellActionButton類型。這就是我們所需要的做文章的view了瓷翻。經(jīng)過探究聚凹,這個(gè)button是在實(shí)例化UITableViewRowAction之后內(nèi)部實(shí)例化的,我們用一個(gè)KVC獲取到該button齐帚,然后自定一個(gè)我們想要的效果妒牙,貼在該button上面即可,注意的是:獲取該button時(shí)不能直接獲取对妄,需要在事件UITableViewRowAction實(shí)例化后延時(shí)零點(diǎn)幾秒之后才能獲取得到

實(shí)現(xiàn)代碼如下

- (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"];
        //自定義一個(gè)view加作為subview加在button上面即可實(shí)現(xiàn)
        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];
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末湘今,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子剪菱,更是在濱河造成了極大的恐慌摩瞎,老刑警劉巖拴签,帶你破解...
    沈念sama閱讀 222,590評(píng)論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異旗们,居然都是意外死亡蚓哩,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,157評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門上渴,熙熙樓的掌柜王于貴愁眉苦臉地迎上來岸梨,“玉大人,你說我怎么就攤上這事稠氮∈⒑伲” “怎么了?”我有些...
    開封第一講書人閱讀 169,301評(píng)論 0 362
  • 文/不壞的土叔 我叫張陵括袒,是天一觀的道長(zhǎng)次兆。 經(jīng)常有香客問我,道長(zhǎng)锹锰,這世上最難降的妖魔是什么芥炭? 我笑而不...
    開封第一講書人閱讀 60,078評(píng)論 1 300
  • 正文 為了忘掉前任,我火速辦了婚禮恃慧,結(jié)果婚禮上园蝠,老公的妹妹穿的比我還像新娘。我一直安慰自己痢士,他們只是感情好彪薛,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,082評(píng)論 6 398
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著怠蹂,像睡著了一般善延。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上城侧,一...
    開封第一講書人閱讀 52,682評(píng)論 1 312
  • 那天易遣,我揣著相機(jī)與錄音,去河邊找鬼嫌佑。 笑死豆茫,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的屋摇。 我是一名探鬼主播揩魂,決...
    沈念sama閱讀 41,155評(píng)論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼炮温!你這毒婦竟也來了火脉?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 40,098評(píng)論 0 277
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎忘分,沒想到半個(gè)月后棋枕,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,638評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡妒峦,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,701評(píng)論 3 342
  • 正文 我和宋清朗相戀三年重斑,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片肯骇。...
    茶點(diǎn)故事閱讀 40,852評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡窥浪,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出笛丙,到底是詐尸還是另有隱情漾脂,我是刑警寧澤,帶...
    沈念sama閱讀 36,520評(píng)論 5 351
  • 正文 年R本政府宣布胚鸯,位于F島的核電站骨稿,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏姜钳。R本人自食惡果不足惜坦冠,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,181評(píng)論 3 335
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望哥桥。 院中可真熱鬧辙浑,春花似錦、人聲如沸拟糕。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,674評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽送滞。三九已至侠草,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間累澡,已是汗流浹背梦抢。 一陣腳步聲響...
    開封第一講書人閱讀 33,788評(píng)論 1 274
  • 我被黑心中介騙來泰國(guó)打工般贼, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留愧哟,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 49,279評(píng)論 3 379
  • 正文 我出身青樓哼蛆,卻偏偏與公主長(zhǎng)得像蕊梧,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子腮介,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,851評(píng)論 2 361

推薦閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理肥矢,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 134,715評(píng)論 18 139
  • 國(guó)家電網(wǎng)公司企業(yè)標(biāo)準(zhǔn)(Q/GDW)- 面向?qū)ο蟮挠秒娦畔?shù)據(jù)交換協(xié)議 - 報(bào)批稿:20170802 前言: 排版 ...
    庭說閱讀 11,010評(píng)論 6 13
  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,322評(píng)論 25 707
  • iOS 11 為整個(gè)生態(tài)系統(tǒng)的 UI 元素帶來了一種更加大膽甘改、動(dòng)態(tài)的新風(fēng)格旅东。 本文介紹iOS11中在UI方面做了哪...
    阿凡提說AI閱讀 591評(píng)論 0 1
  • 還記得小時(shí)候?qū)W習(xí)書法嗎抵代?描紅了一遍又一遍,再對(duì)照著寫一遍又一遍忘嫉,學(xué)習(xí)素描也是先對(duì)照著參照物畫荤牍,對(duì)著畫一次又一次,再...
    遇上緣閱讀 171評(píng)論 0 1