iOS封装checkbox

 转载说明出处。否则祝你明天被炒。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 {

    

    //找到对应的keyvalue并改变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;

        }

    }

    

    //从字典中筛选出valueYES对应的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]);

        }

    };

    用到的图片



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值