[圖片上傳失敗...(image-10d99d-1510544028651)]
<a class="btn btn-success follow" style="box-sizing: border-box; background-color: rgb(66, 192, 46); color: rgb(255, 255, 255); text-decoration: none; cursor: pointer; display: inline-block; margin-bottom: 0px; font-weight: 400; text-align: center; vertical-align: middle; touch-action: manipulation; background-image: none; border: 1px solid rgb(66, 192, 46); white-space: nowrap; padding: 0px 7px 0px 5px; font-size: 12px; line-height: normal; border-radius: 40px; user-select: none;">關(guān)注</a>
2016.08.20 10:31*
字?jǐn)?shù) 555
閱讀 5039
評(píng)論 4
喜歡 4
一彼念、思路解析
充分利用button的select屬性槽畔,設(shè)置button的選中及非選中狀態(tài)局嘁。設(shè)置一個(gè)全局的選中button(當(dāng)前選中的button),這樣一個(gè)簡單的單選功能就實(shí)現(xiàn)了端朵。
二、具體源碼
(1)準(zhǔn)備工作:
@property (nonatomic,strong) UIView *SearchTopview;
@property (nonatomic,weak) UIButton *Selectbutton;
pragma mark - getter and setter
- (UIView *)SearchTopview{
if (_SearchTopview == nil) {
_SearchTopview = [[UIView alloc] init];
_SearchTopview.frame = CGRectMake(0, 0, SCREEN_WIDTH, 100);
}
return _SearchTopview;
}
(2)首先一個(gè)for循環(huán)設(shè)置按鈕
UILabel *lbTitle = [[UILabel alloc]init];
lbTitle.frame = CGRectMake(10, 0, SCREEN_WIDTH - 20, 30);
lbTitle.textAlignment = NSTextAlignmentLeft;
lbTitle.text = @"選擇你要的搜索分類";
lbTitle.textColor = [UIColor grayColor];
lbTitle.font = HiraginoMidFONT;
[self.SearchTopview addSubview:lbTitle];
NSArray *titlearray = @[@"資訊",@"餐答",@"秀場(chǎng)",@"廚師",@"掌柜",@"秘籍",@"課堂",@"產(chǎn)品",@"懸賞",@"人物志"];
int col = 4;
int margin = 10;
for (int i = 0; i < titlearray.count; i++) {
int page = i/col;
int index = i%col;
UIButton BtnSearch = [[UIButton alloc]initWithFrame:CGRectMake(margin + index(SCREEN_WIDTH - (col + 1)margin)/col + marginindex,40page + 40,(SCREEN_WIDTH - (col + 1)margin)/col,30 )];
BtnSearch.layer.cornerRadius = 5;
BtnSearch.layer.masksToBounds = YES;
BtnSearch.layer.shadowOffset = CGSizeMake(1, 1);
BtnSearch.layer.shadowOpacity = 0.8;
BtnSearch.layer.shadowColor = [UIColor blackColor].CGColor;
BtnSearch.backgroundColor = [UIColor lightGrayColor];
BtnSearch.tag = i;
[BtnSearch setTitle:titlearray[i] forState:UIControlStateNormal];
[BtnSearch setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[BtnSearch setTitleColor:BackyellowColor forState:UIControlStateSelected];
BtnSearch.titleLabel.font = HiraginoMidFONT;
[BtnSearch addTarget:self action:@selector(SelectBtnSearch:) forControlEvents:UIControlEventTouchUpInside];
if (i == 0) {
BtnSearch.backgroundColor = BackblackColor;
BtnSearch.selected = YES;
self.Selectbutton = BtnSearch;
}
[self.SearchTopview addSubview:BtnSearch];
}
self.SearchTopview.height = [CalculationTool rowWithcount:titlearray.count andcol:col]*40 + 45;
(3)按鈕事件
pragma mark - event
- (void)SelectBtnSearch:(UIButton *)Btn{
if (!Btn.isSelected) {
self.Selectbutton.selected = !self.Selectbutton.selected;
self.Selectbutton.backgroundColor = [UIColor lightGrayColor];
Btn.selected = !Btn.selected;
Btn.backgroundColor = BackblackColor;
self.Selectbutton = Btn;
}
}
(4)按鈕事件 簡述
if (!Btn.isSelected):使點(diǎn)擊當(dāng)前按鈕是不執(zhí)行事件
self.Selectbutton.selected = !self.Selectbutton.selected; Btn.selected = !Btn.selected;
已經(jīng)選中的按鈕(self.Selectbutton)及當(dāng)前點(diǎn)擊按鈕(Btn)的取反 使選中按鈕為绞灼!select狀態(tài)唱蒸,單擊按鈕為selectt狀態(tài)
self.Selectbutton = Btn; 所以現(xiàn)在的選中按鈕就替換為你現(xiàn)在所點(diǎn)擊的按鈕了。
一個(gè)簡單的單選按鈕就實(shí)現(xiàn)了础芍。 第一次嘗試寫文章分享杈抢,文筆啥的差了,歡迎點(diǎn)評(píng)仑性,先來個(gè)簡單的試試水哈惶楼、。