转载说明出处。否则祝你明天被炒。github下载地址 https://github.com/MRYIN123/CheckBox.git有点错误 不能运行但是把工程文件直接拉到代码里就可以用了
工作中用到checkbox,所以封装一个
#import "CheckBoxView.h"
#define Screen_height ([UIScreen mainScreen].bounds.size.height)
@interface CheckBoxView(){
BOOL isScroller;
BOOL isCheckVertical;
NSMutableDictionary *dic;
NSArray *array_check;
UIScrollView *scrollerView;
}
@end
@implementation CheckBoxView
- (id)initViewWithFrame:(CGRect)frame CheckArray:(NSArray *)checkArray Vertical:(BOOL)isVertical {
if (self) {
self = [superinitWithFrame:frame];
array_check = [NSArrayarrayWithArray:checkArray];
isCheckVertical = isVertical;
//scrollerView
scrollerView = [[UIScrollViewalloc]initWithFrame:CGRectMake(0,0, frame.size.width, frame.size.height)];
scrollerView.showsHorizontalScrollIndicator =NO;
scrollerView.showsVerticalScrollIndicator =NO;
[selfaddSubview:scrollerView];
//用dic来存储每个元素是否选中的状态
dic = [NSMutableDictionarydictionary];
for (int i =0 ; i < checkArray.count; i++) {
[dicsetObject:@(NO)forKey:[NSStringstringWithFormat:@"%d",i]];
float x,y,height, width;
if (isVertical) {
width = frame.size.width;
height = frame.size.height / checkArray.count;
y = i * height;
x = 5;
}else{
height = frame.size.height;
width = frame.size.width / checkArray.count ;
x = i * width ;
y = 0;
}
//初始化view
UIView *miniView = [selfcreateCheckMiniViewWithFrame:CGRectMake(x, y, width, height)Title:checkArray[i]CheckCount:i];
miniView.tag =1000 + i;
[scrollerViewaddSubview:miniView];
}
}
returnself;
}
/*
* 创建view的方法
*/
- (UIView *)createCheckMiniViewWithFrame:(CGRect)frame Title:(NSString *)title CheckCount:(NSInteger)checkCount {
UIView *view = [[UIViewalloc]initWithFrame:frame];
UIButton * btn_img = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];
[btn_img setBackgroundImage:[UIImageimageNamed:@"Btn_Checkbox_Uncheck"]forState:UIControlStateNormal];
btn_img.tag =2000;
btn_img.frame =CGRectMake(5, (CGRectGetHeight(frame)-20)/2,20,20);
[view addSubview:btn_img];
[btn_img addTarget:selfaction:@selector(selectCheckBoxAction:)forControlEvents:UIControlEventTouchUpInside];
UIButton * btn_content = [UIButtonbuttonWithType:UIButtonTypeSystem];
btn_content.frame =CGRectMake(CGRectGetMaxX(btn_img.frame) +5, 0, frame.size.width -CGRectGetMaxX(btn_img.frame),CGRectGetHeight(frame));
btn_content.tintColor = [UIColorblackColor];
[btn_content setTitle:titleforState:UIControlStateNormal];
[btn_content setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
[view addSubview:btn_content];
[btn_content addTarget:selfaction:@selector(selectCheckBoxAction:)forControlEvents:UIControlEventTouchUpInside];
return view;
}
/*
*点击事件
*/
- (void)selectCheckBoxAction:(UIButton *)sender {
//找到对应的key和value并改变value值
for (NSString *keyindic) {
NSInteger tag = sender.superview.tag -1000;
BOOL isSelect;
if ([keyintegerValue] == tag) {
isSelect = [[dicobjectForKey:key]boolValue];
if (isSelect) {
[[sender.superviewviewWithTag:2000]setBackgroundImage:[UIImageimageNamed:@"Btn_Checkbox_Uncheck"]forState:UIControlStateNormal];
}else{
[[sender.superviewviewWithTag:2000]setBackgroundImage:[UIImageimageNamed:@"Btn_Checkbox_Check"]forState:UIControlStateNormal];
}
isSelect = !isSelect;
[dicsetObject:@(isSelect)forKey:[NSStringstringWithFormat:@"%ld",tag]];
break;
}
}
//从字典中筛选出value是YES对应的index存入数组
if (self.block_select) {
NSMutableArray * selectArray = [NSMutableArrayarray];
for (NSString *keyindic) {
BOOL select = [[dicvalueForKey:key]boolValue];
if (select) {
[selectArray addObject:key];
}
}
self.block_select(selectArray);
}
//从字典中筛选出选择的内容存入数组
if (self.block_selectContent) {
NSMutableArray * selectArray = [NSMutableArrayarray];
for (NSString *keyindic) {
BOOL select = [[dicvalueForKey:key]boolValue];
if (select) {
for (int i =0; i <array_check.count; i++) {
if (i == [keyintegerValue]) {
if (array_check[i]) {
[selectArrayaddObject:array_check[i]];
}
}
}
}
}
self.block_selectContent(selectArray);
}
}
/*
*设置是否可以滑动
*/
- (void)setIsScroller
{
scrollerView.scrollEnabled =YES;
if (isCheckVertical) {
scrollerView.contentSize =CGSizeMake(CGRectGetWidth(scrollerView.frame),CGRectGetHeight(scrollerView.frame) +50);
}else{
scrollerView.contentSize =CGSizeMake(CGRectGetWidth(scrollerView.frame) +50, CGRectGetHeight(scrollerView.frame));
}
}
@end
//横向checkbox不会滚动
NSArray *array1 = [NSArrayarrayWithObjects:@"苹果",@"三星",@"小米",nil];
CheckBoxView *view1 = [[CheckBoxViewalloc]initViewWithFrame:CGRectMake(30,40,200,40)CheckArray:array1Vertical:NO];
view1.backgroundColor = [UIColorcolorWithWhite:0.9alpha:4];
[self.viewaddSubview:view1];
//返回选择下标的数组
view1.block_select = ^(NSMutableArray *arr){
for (int i =0; i < arr.count; i++) {
NSLog(@"%@",arr[i]);
}
};
//返回选择内容的数组
view1.block_selectContent = ^(NSMutableArray *arr){
for (int i =0; i < arr.count; i++) {
NSLog(@"%@",arr[i]);
}
};
用到的图片