一你画、效果gif
二抵碟、代碼的實(shí)現(xiàn)
自己思路1:為每個(gè)按鈕添加點(diǎn)擊事件桃漾,根據(jù)屬性button.selecteds。這個(gè)方法簡(jiǎn)單方便
自己思路2:實(shí)現(xiàn)多個(gè)復(fù)選框的選擇與取消的效果拟逮。首先需要為每一個(gè)按鈕
添加一個(gè)屬性,用來(lái)記錄按鈕實(shí)時(shí)處于的狀態(tài)(選中還是撬统、未選中的狀態(tài)),然后根據(jù)按鈕的狀態(tài)來(lái)對(duì)按鈕進(jìn)行操作敦迄。這個(gè)方法想對(duì)來(lái)說(shuō)較復(fù)雜
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];
//重點(diǎn)來(lái)了,操作屬性的設(shè)置
- (void)changeBackground:(UIButton *)btn{
btn.selected = !btn.selected;
if (btn.selected) {
btn.backgroundColor = [UIColor yellowColor];
}else{
btn.backgroundColor = [UIColor redColor];
}
}
2.1思路2(通過(guò)運(yùn)行時(shí)
為按鈕添加屬性)效果2
UIButton+SelectStateBtn.h
#import <UIKit/UIKit.h>
@interface UIButton (SelectStateBtn)
//為UIButton添加屬性值selectStateBtn恋追,來(lái)記錄按鈕的狀態(tài)
@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;//行號(hào)
NSInteger row = i%3;//列號(hào)
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:@"市場(chǎng)%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];
}
//多個(gè)復(fù)選框的選中方法
- (void)add:(UIButton *)btn{
if ([btn.selectStateBtn isEqualToString:@"1"] &&btn.selectStateBtn.length != 0) {
btn.backgroundColor = [UIColor whiteColor];
//用字符串0代表已經(jīng)選中的狀態(tài)
btn.selectStateBtn = @"0";
}else{
btn.backgroundColor = [UIColor yellowColor];
//用字符串1代表已經(jīng)選中的狀態(tài)
btn.selectStateBtn = @"1";
}
}
文章已經(jīng)寫完了,歡迎大家批評(píng)指正罚屋。我這邊改變的只是背景 圖片苦囱,最好的方式:美工那邊提供二張照片,選中狀態(tài)下和為選中的狀態(tài)下的圖片脾猛。這樣效果最好撕彤,我這里只是做個(gè)測(cè)試。猛拴。有什么問(wèn)題:請(qǐng)聯(lián)系我的QQ:
1312940166
.star
star
star