ActionSheet框架是iOS平台非常好用的一个d框三方库(GitHub - domanc/ActionSheet: 仿微信ActionSheet),作者模拟类似微信的d框效果,上手是非常方便的,相比系统提供的更扁平化一些,深得人心,具体效果如下:
使用方法也是非常简单,这里笔者列出自己产品的相关代码如下:
kWEAK_SELF
ActionSheetView *sheetView = [[ActionSheetView alloc] initWithCancleTitle:@"取消" otherTitles:model.authcode,@"修改",@"复制",@"删除",nil];
[sheetView show];
sheetView.clickBlock = ^(ActionSheetItem *item){
switch (item.index) {
case 2:
{
[weakSelf showDatePickerView:model];
}
break;
case 3:
{
[weakSelf pasteboardAuthCode:model];
}
break;
case 4:
{
NSString *message = [NSString stringWithFormat:@"确定删除【 %@ 】吗?",model.authcode];
[UIAlertView showWithTitle:nil message:message cancelButtonTitle:@"取消" otherButtonTitles:@[@"确定"] tapBlock:^(UIAlertView * _Nonnull alertView, NSInteger buttonIndex) {
switch (buttonIndex) {
case 1:
{
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[weakSelf delAuthCode:model];
});
}
break;
}
}];
}
break;
}
};
问题发现:
从作者源码可以看出,这个多选的按钮是以C语言VA_LIST的方式来实现的,来解决传参数量的问题,也是非常友好的,代码如下:
- (instancetype)initWithCancleTitle:(NSString *)cancleTitle
otherTitles:(NSString *)otherTitles,... NS_REQUIRES_NIL_TERMINATION;
查看相关源码如下:
NSString* curStr;
va_list list;
if(otherTitles)
{
ActionSheetItem *item = [ActionSheetItem itemWithTitle:otherTitles index:tag];
[_items addObject:item];
tag ++;
va_start(list, otherTitles);
while ((curStr = va_arg(list, NSString*))) {
ActionSheetItem *item = [ActionSheetItem itemWithTitle:curStr index:tag];
[_items addObject:item];
tag ++;
}
va_end(list);
}
但是,如果我们需要d出的数据是从服务器获取,也就是说这个是动态的,那又该如何使用这个框架呢?
va_list与NSAarry转换有点不伦不类,那么,笔者经过一番思考,决定改写成动态传参的方式,相比作者的静态传参更符合笔者项目的需要,毕竟这个d窗数据是动态获取的。
为了不影响之前的代码,笔者重新创建一个初始化方法,完整代码如下:
- (instancetype)initWithCancleTitle:(NSString *)cancleTitle otherTitlesArr:(NSArray *)otherTitles{
self = [super init];
if (self) {
//黑色遮盖
self.frame = [UIScreen mainScreen].bounds;
self.backgroundColor = [UIColor blackColor];
[[UIApplication sharedApplication].keyWindow addSubview:self];
self.alpha = 0.0;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(coverClick)];
[self addGestureRecognizer:tap];
// sheet
_sheetView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 0)];
_sheetView.backgroundColor = ActionSheetColor(236,239,240);
_sheetView.alpha = 1.0;
[[UIApplication sharedApplication].keyWindow addSubview:_sheetView];
_sheetView.hidden = YES;
int tag = 0;
_items = [NSMutableArray array];
//首先添加取消按钮
ActionSheetItem *cancleItem = [ActionSheetItem itemWithTitle:@"取消" index:0];
[_items addObject:cancleItem];
tag ++;
for (NSString *title in otherTitles) {
ActionSheetItem *item = [ActionSheetItem itemWithTitle:title index:tag];
[_items addObject:item];
tag ++;
}
CGRect sheetViewF = _sheetView.frame;
sheetViewF.size.height = BtnHeight * _items.count + CancleMargin;
_sheetView.frame = sheetViewF;
//开始添加按钮
[self setupBtnWithTitles];
}
return self;
}
使用方法:
NSMutableArray *titleArr = [NSMutableArray arrayWithCapacity:8];
for (OptionModel *optionModel in self.optionsArr) {
[titleArr addObject:optionModel.dis];
}
kWEAK_SELF
ActionSheetView *sheetView = [[ActionSheetView alloc] initWithCancleTitle:@"取消" otherTitlesArr:titleArr];
[sheetView show];
sheetView.clickBlock = ^(ActionSheetItem *item){
OptionModel *optionModel = [self.optionsArr objectAtIndex:item.index-1];
NSLog(@"optionModel.dis:%@",optionModel.dis);
[weakSelf randomAuthCode:optionModel];
};
这样就即支持了原有的静态变参传值,也新增了动态数组传值,让优秀的框架更自由,更美好,代码地址:GitHub - mapboo/ActionSheet-Array: 基于https://github.com/domanc/ActionSheet新增数组传值,最后再次感谢原作者(GitHub - domanc/ActionSheet: 仿微信ActionSheet)做出的贡献。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)