做 iOS 開發的朋友大概都有過這樣的體驗:想讓一個 UIButton 依據不同狀態顯示不同背景色,下意識地去找 setBackgroundColor:forState: 這樣的方法,結果翻遍文檔才發現——系統根本沒提供。於是只能自己動手,把這塊缺失的拼圖補上。
其實 UIButton 早就給了我們一個近似的思路。觀察它設置背景圖片的方式,就能明白蘋果的設計邏輯:
- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state UI_APPEARANCE_SELECTOR; // default is nil - (UIImage *)backgroundImageForState:(UIControlState)state;
既然圖片能按狀態存取,那背景色照搬這套模式即可。我們先規劃好對外暴露的介面:
- (void)setBackgroundColor:(UIColor *)color forState:(UIControlState)state; - (UIColor *)backgroundColorForState:(UIControlState)state;
接下來是具體實作。核心思路並不複雜:用一個字典按狀態保存顏色,再即時接管狀態切換時的背景更新。聲明部分如下:
@interface WMButton : UIButton @property (nonatomic, copy) NSString *name; - (void)setBackgroundColor:(UIColor *)backgroundColor forState:(UIControlState)state; - (UIColor *)backgroundColorForState:(UIControlState)state; @end
實作程式碼如下,邏輯寫得很直白,就不多贅述了:
@implementation WMButton { NSMutableDictionary *_colors; } - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { _colors = [NSMutableDictionary dictionary]; } return self; } - (void)setBackgroundColor:(UIColor *)backgroundColor forState:(UIControlState)state { // If it is normal then set the standard background here if(state == UIControlStateNormal) { [super setBackgroundColor:backgroundColor]; } // Store the background colour for that state [_colors setValue:backgroundColor forKey:[self keyForState:state]]; } - (UIColor *)backgroundColorForState:(UIControlState)state { return [_colors valueForKey:[self keyForState:state]]; } - (void)setHighlighted:(BOOL)highlighted { // Do original Highlight [super setHighlighted:highlighted]; // Highlight with new colour OR replace with orignial NSString *highlightedKey = [self keyForState:UIControlStateHighlighted]; UIColor *highlightedColor = [_colors valueForKey:highlightedKey]; if (highlighted && highlightedColor) { [super setBackgroundColor:highlightedColor]; } else { // 由於系統在調用setSelected後,會再觸發一次setHighlighted,故做如下處理,否則,背景色會被最後一次的覆蓋掉。 if ([self isSelected]) { NSString *selectedKey = [self keyForState:UIControlStateSelected]; UIColor *selectedColor = [_colors valueForKey:selectedKey]; [super setBackgroundColor:selectedColor]; } else { NSString *normalKey = [self keyForState:UIControlStateNormal]; [super setBackgroundColor:[_colors valueForKey:normalKey]]; } } } - (void)setSelected:(BOOL)selected { // Do original Selected [super setSelected:selected]; // Select with new colour OR replace with orignial NSString *selectedKey = [self keyForState:UIControlStateSelected]; UIColor *selectedColor = [_colors valueForKey:selectedKey]; if (selected && selectedColor) { [super setBackgroundColor:selectedColor]; } else { NSString *normalKey = [self keyForState:UIControlStateNormal]; [super setBackgroundColor:[_colors valueForKey:normalKey]]; } } - (NSString *)keyForState:(UIControlState)state { return [NSString stringWithFormat:@"state_%d", state]; } @end使用起來相當直觀,一行一個狀態,一目了然:
[button setBackgroundColor:[UIColor clearColor] forState:UIControlStateNormal]; [button setBackgroundColor:[UIColor clearColor] forState:UIControlStateHighlighted]; [button setBackgroundColor:HotPinkColor forState:UIControlStateSelected];
不過有個地方要多提醒一句。有些人可能會想著這樣寫:
[button setBackgroundColor:HotPinkColor forState:UIControlStateHighlighted | UIControlStateSelected];
這種把多個狀態用位運算合併的寫法,在這裡是不被支援的。其實即便是系統原生的 setBackgroundImage:forState:,對於這類組合狀態的用法 likewise 支援得並不完整,有興趣的可以自己動手試試。
說到這裡,我們不妨再深入聊聊 UIButton 切換 state 的執行順序,因為它直接影響到上面的實作是否可靠。
當 UIButton 被按下時,系統會啟動一個計時器,週期性地檢測手指是否還停留在按鈕上。若仍在按壓,就切換到 UIControlStateHighlighted;否則回到 UIControlStateNormal。而當你長按之後,在按鈕區域內抬起手指,狀態會切換到 UIControlStateSelected——關鍵在於,這一次切換不只會觸發 setSelected:,還會順帶觸發一次 setHighlighted:。下面這段 Log 就把這個過程記錄了下來:
2015-01-08 19:43:58.782 V |: highlighted = 1 @ WMButton(#38).-[WMButton setHighlighted:] 2015-01-08 19:44:00.227 V |: highlighted = 1 @ WMButton(#38).-[WMButton setHighlighted:] 2015-01-08 19:44:00.277 V |: highlighted = 1 @ WMButton(#38).-[WMButton setHighlighted:] 2015-01-08 19:44:00.294 V |: highlighted = 1 @ WMButton(#38).-[WMButton setHighlighted:] 2015-01-08 19:44:00.327 V |: highlighted = 1 @ WMButton(#38).-[WMButton setHighlighted:] 2015-01-08 19:44:00.344 V |: highlighted = 1 @ WMButton(#38).-[WMButton setHighlighted:] 2015-01-08 19:44:00.377 V |: highlighted = 1 @ WMButton(#38).-[WMButton setHighlighted:] 2015-01-08 19:44:00.494 V |: highlighted = 1 @ WMButton(#38).-[WMButton setHighlighted:] 2015-01-08 19:44:00.528 V |: highlighted = 1 @ WMButton(#38).-[WMButton setHighlighted:] 2015-01-08 19:44:00.577 V |: highlighted = 1 @ WMButton(#38).-[WMButton setHighlighted:] 2015-01-08 19:44:00.660 V |: highlighted = 1 @ WMButton(#38).-[WMButton setHighlighted:] 2015-01-08 19:44:00.777 V |: highlighted = 1 @ WMButton(#38).-[WMButton setHighlighted:] 2015-01-08 19:44:00.894 V |: highlighted = 1 @ WMButton(#38).-[WMButton setHighlighted:] 2015-01-08 19:44:00.927 V |: highlighted = 1 @ WMButton(#38).-[WMButton setHighlighted:] 2015-01-08 19:44:00.944 V |: highlighted = 1 @ WMButton(#38).-[WMButton setHighlighted:] 2015-01-08 19:44:01.127 V |: highlighted = 1 @ WMButton(#38).-[WMButton setHighlighted:] 2015-01-08 19:44:01.661 V |: highlighted = 1 @ WMButton(#38).-[WMButton setHighlighted:] 2015-01-08 19:44:01.696 V |: highlighted = 1 @ WMButton(#38).-[WMButton setHighlighted:] 2015-01-08 19:44:01.762 V |: highlighted = 1 @ WMButton(#38).-[WMButton setHighlighted:] 2015-01-08 19:44:01.795 V |: highlighted = 1 @ WMButton(#38).-[WMButton setHighlighted:] 2015-01-08 19:44:01.862 V |: highlighted = 1 @ WMButton(#38).-[WMButton setHighlighted:] 2015-01-08 19:44:01.895 V |: highlighted = 1 @ WMButton(#38).-[WMButton setHighlighted:] 2015-01-08 19:44:01.930 V |: highlighted = 1 @ WMButton(#38).-[WMButton setHighlighted:] 2015-01-08 19:44:03.961 V |: highlighted = 1 @ WMButton(#38).-[WMButton setHighlighted:] 2015-01-08 19:44:05.162 V |: highlighted = 1 @ WMButton(#38).-[WMButton setHighlighted:]
2015-01-08 19:44:05.328 V |: highlighted = 1 @ WMButton(#38).-[WMButton setHighlighted:]
2015-01-08 19:44:05.346 V |: selected = 1 @ WMButton(#56).-[WMButton setSelected:]
2015-01-08 19:44:05.347 V |: highlighted = 0 @ WMButton(#38).-[WMButton setHighlighted:]