iOS11中tableview的左右滑動(dòng)事件葬馋,iOS更新了左右滑動(dòng)事件,新增添了兩個(gè)方法孕暇,可以為事件自定義添加圖片和標(biāo)題。不過有點(diǎn)限制聋袋,就是圖片和標(biāo)題都會(huì)變成白色,不能顯示原圖和設(shè)置標(biāo)題大小和顏色穴吹。效果如下
進(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);
}
明顯的沒有我們想要的UIImageView和UILabel落午,說明UIContextualAction并沒有直接持有他們作為屬性。用KVC的方法獲取不到肚豺,自然也就改不了溃斋。
iOS11之前的左右劃事件
iOS11之前默認(rèn)的劃動(dòng)事件是不能直接設(shè)置圖標(biāo)的,只能設(shè)置標(biāo)題和背景顏色详炬。先看看效果
實(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)和改字體的大小和顏色呢呛谜?如以下效果我們看看事件的響應(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);
}
從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];
}