活用UIButton的狀態(tài)設(shè)置

前言:項目中,設(shè)置UIButton的顏色,背景來顯示不同的狀態(tài)是很常見的操作,比如:不可點(diǎn)置灰,點(diǎn)擊高亮等等。下面,記錄一下,在這方面踩的的坑。

1.UIButton可以設(shè)置4種狀態(tài)

typedef NS_OPTIONS(NSUInteger, UIControlState) {
    UIControlStateNormal       = 0,
    UIControlStateHighlighted  = 1 << 0,                  // used when UIControl isHighlighted is set
    UIControlStateDisabled     = 1 << 1,
    UIControlStateSelected     = 1 << 2,                  // flag usable by app (see below)
}

UIControlState是一個options而不是枚舉,也就是說,這些狀態(tài)可以組合,這就可能存在相互影響的問題,這就是UIControlState的大坑。

2.簡要介紹一下四種state所代表的意義:

  • UIControlStateNormal

The normal, or default state of a control—that is, enabled but neither selected nor highlighted.
代表enable,unselected,unhighlight的狀態(tài),這也是個坑,normal是一切你沒有設(shè)置的特殊狀態(tài)都顯示normal的狀態(tài),不僅僅是這個。

  • UIControlStateHighlighted

Highlighted state of a control. A control becomes highlighted when a touch event enters the control’s bounds, and it loses that highlight when there is a touch-up event or when the touch event exits the control’s bounds.
highlight只和事件有關(guān),比如,當(dāng)進(jìn)入點(diǎn)擊范圍時會高亮,出去點(diǎn)擊范圍時高亮?xí)?,但前提下,這個UIButton可點(diǎn)擊,否則沒有意義。

  • UIControlStateDisabled

Disabled state of a control. User interactions with disabled control have no effect and the control draws itself with a dimmed appearance to reflect that it is disabled.
這個當(dāng)UIButton不可點(diǎn)擊的時候觸發(fā)

  • UIControlStateSelected

Selected state of a control. For many controls, this state has no effect on behavior or appearance.
UIButton默認(rèn)selected是NO

3.重點(diǎn)介紹這四種狀態(tài)的各種顯示

UIControlState是BitMask,3 個有效的 Bit 組合其實可以擁有 8 種狀態(tài),分別是:

0 0 0 => UIControlStateNormal
0 0 1 => UIControlStateHighlighted
0 1 0 => UIControlStateDisabled
0 1 1 => UIControlStateHighlighted & UIControlStateDisabled
1 0 0 => UIControlStateSelected
1 0 1 => UIControlStateSelected & UIControlStateHighlighted
1 1 0 => UIControlStateSelected & UIControlStateDisabled
1 1 1 => UIControlStateSelected & UIControlStateDisabled & UIControlStateHighlighted

//設(shè)置UIButton的8種狀態(tài)
[btn setTitle: @"Normal" forState: UIControlStateNormal];
[btn setTitle: @"Highlighted" forState: UIControlStateHighlighted];
[btn setTitle: @"Disabled" forState: UIControlStateDisabled];
[btn setTitle: @"Highlighted & Disabled" forState: UIControlStateHighlighted | UIControlStateDisabled];
[btn setTitle: @"Selected" forState: UIControlStateSelected];
[btn setTitle: @"Selected & Highlighted" forState:UIControlStateSelected | UIControlStateHighlighted];
[btn setTitle: @"Selected & Disabled" forState:UIControlStateSelected | UIControlStateDisabled];
[btn setTitle: @"Selected & Highlighted & Disabled" forState: UIControlStateSelected | UIControlStateHighlighted | UIControlStateDisabled];

//設(shè)置不同的state,看UIButton的顯示
//normal
UIButton *button1 = [self getButton];
button1.center = CGPointMake(center.x, base);
[self.view addSubview: button1];

//highlight
UIButton *button2 = [self getButton];
button2.center = CGPointMake(center.x, base + space);
button2.highlighted = YES;
[self.view addSubview: button2];

//disable
UIButton *button3 = [self getButton];
button3.center = CGPointMake(center.x, base + space * 2);
button3.enabled = NO;
[self.view addSubview: button3];

//highlight & disable
UIButton *button4 = [self getButton];
button4.center = CGPointMake(center.x, base + space * 3);
button4.highlighted = YES;
button4.enabled = NO;
[self.view addSubview: button4];

//selected
UIButton *button5 = [self getButton];
button5.center = CGPointMake(center.x, base + space * 4);
button5.selected = YES;
[self.view addSubview: button5];

//selected & highlight
UIButton *button6 = [self getButton];
button6.center = CGPointMake(center.x, base + space * 5);
button6.selected = YES;
button6.highlighted = YES;
[self.view addSubview: button6];

//selected & disable
UIButton *button7 = [self getButton];
button7.center = CGPointMake(center.x, base + space * 6);
button7.selected = YES;
button7.enabled = NO;
[self.view addSubview: button7];

//highlight & disable & selected
UIButton *button8 = [self getButton];
button8.center = CGPointMake(center.x, base + space * 7);
button8.highlighted = YES;
button8.enabled = NO;
button8.selected = YES;
[self.view addSubview: button8];

有圖有真相:

UIControlState.png

結(jié)果是:
1.UIControlStateHighlighted & UIControlStateDisabled**會覆蓋UIControlStateHighlighted的狀態(tài) 2.UIControlStateSelected & UIControlStateHighlighted & UIControlStateDisabled會覆蓋UIControlStateSelected & UIControlStateHighlighted 3.也就說highlight和disable在一起,不管該UIButton是否可點(diǎn),都是顯示highlight和disable

總結(jié):

1.使用UIControlState盡量精確,比如說:可點(diǎn)狀態(tài)下已選擇的高亮
2.注意高亮和不可以點(diǎn)擊狀態(tài)的組合

參考:

1.《UIButton 狀態(tài)的新理解》
2.apple文檔

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容