iOS多个复选框的选中、取消的效果

2016-06-27  本文已影响3527人  Xcode8

一、效果gif

效果1.gif 效果2.gif

二、代码的实现

自己思路1:为每个按钮添加点击事件,根据属性button.selecteds。这个方法简单方便
自己思路2:实现多个复选框的选择与取消的效果。首先需要为每一个按钮添加一个属性,用来记录按钮实时处于的状态(选中还是、未选中的状态),然后根据按钮的状态来对按钮进行操作。这个方法想对来说较复杂

2.1思路1(操作属性button.selected)效果1

//按钮1
UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
btn.frame = CGRectMake(100, 100, 100, 100);
btn.backgroundColor = [UIColor redColor];
[btn setTitle:@"666666" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(changeBackground:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];

//按钮2
UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeSystem];
btn1.frame = CGRectMake(200, 200, 100, 100);
btn1.backgroundColor = [UIColor redColor];
[btn1 setTitle:@"888888" forState:UIControlStateNormal];
[btn1 addTarget:self action:@selector(changeBackground:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn1];

//重点来了,操作属性的设置
- (void)changeBackground:(UIButton *)btn{
    btn.selected = !btn.selected;
    if (btn.selected) {
        btn.backgroundColor = [UIColor yellowColor];
    }else{
        btn.backgroundColor = [UIColor redColor];
    }

}

2.1思路2(通过运行时为按钮添加属性)效果2

UIButton+SelectStateBtn.h
  #import <UIKit/UIKit.h>
  @interface UIButton (SelectStateBtn)
//为UIButton添加属性值selectStateBtn,来记录按钮的状态
  @property NSString *selectStateBtn;
  - (void)setSelectStateBtn:(NSString *)selectStateBtn;
  - (NSString *)selectStateBtn;
  @end

UIButton+SelectStateBtn.m
#import "UIButton+SelectStateBtn.h"
#import <objc/runtime.h>
static void *tagKey = &tagKey;
@implementation UIButton (SelectStateBtn)
- (void)setSelectStateBtn:(NSString *)selectStateBtn{
     objc_setAssociatedObject(self, tagKey, selectStateBtn, OBJC_ASSOCIATION_COPY_NONATOMIC);
}
- (NSString *)selectStateBtn{
    return objc_getAssociatedObject(self, tagKey);
}
@end

开始使用的地方

//添加button
    for (int i = 0; i < 9; i ++) {
        NSInteger line = i/3;//行号
        NSInteger row = i%3;//列号
        NSInteger space = (KWidth-3*100)/4;
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(row*(space+100)+space, line*(space+30)+120, 100, 30);
        [btn setTitle:[NSString stringWithFormat:@"市场%d",i] forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont systemFontOfSize:12];
        btn.layer.borderWidth = 0.5f;
        btn.layer.borderColor = [[UIColor grayColor] CGColor];
        [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(add:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:btn];  
}


//多个复选框的选中方法
- (void)add:(UIButton *)btn{
    if ([btn.selectStateBtn isEqualToString:@"1"] &&btn.selectStateBtn.length != 0) {
        btn.backgroundColor = [UIColor whiteColor];
        //用字符串0代表已经选中的状态
         btn.selectStateBtn = @"0";
    }else{
      btn.backgroundColor = [UIColor yellowColor];
        //用字符串1代表已经选中的状态
        btn.selectStateBtn = @"1";
}  

}

文章已经写完了,欢迎大家批评指正。我这边改变的只是背景 图片,最好的方式:美工那边提供二张照片,选中状态下和为选中的状态下的图片。这样效果最好,我这里只是做个测试。。有什么问题:请联系我的QQ:1312940166. star star star

上一篇下一篇

猜你喜欢

热点阅读