關(guān)于UIButton設(shè)置selected為YES時,再次長按UIButton時出現(xiàn)的問題

按鈕在 selected 為 YES 時,再次點擊或長按,按鈕的文字變成 normal 狀態(tài)的了。

代碼:

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(50, 100, 100, 40);
button.backgroundColor = [UIColor blackColor];
[button setTitle:@"normal" forState:UIControlStateNormal];
[button setTitle:@"selected" forState:UIControlStateSelected];
button.selected = YES;
[self.view addSubview:button];

效果:


點擊或長按的效果

首先看一下UIButton的UIControlState的枚舉

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)
    UIControlStateFocused NS_ENUM_AVAILABLE_IOS(9_0) = 1 << 3, // Applicable only when the screen supports focus
    UIControlStateApplication  = 0x00FF0000,              // additional flags available for application use
    UIControlStateReserved     = 0xFF000000               // flags reserved for internal framework use
};

按鈕的狀態(tài)是位枚舉,不是普通的枚舉,也就是說按鈕可以有不止一個狀態(tài)。所以當按鈕的selected為YES時,再點擊的時候狀態(tài)應(yīng)該不是 normal 狀態(tài)而是其他狀態(tài)。

發(fā)現(xiàn)了相關(guān)的說明:UIButton control states

So why would you care about this? Say you were implementing an edit button or 
a selection button with text, a background image and an image. You want different 
text and images for the selected and unselected states (e.g. Editand Done), 
but you also want to modify the images or background images when highlighting -
if you’re creating your own theme for the app then the default darkening or 
dimming might not be what you want.
So you actually need four images - normal, highlighted, selected 
and selected + highlighted.
This is achieved like so:

[self.button setImage:normal     forState:UIControlStateNormal];
[self.button setImage:highlighted forState:UIControlStateHighlighted];
[self.button setImage:selected forState:UIControlStateSelected];
[self.button setImage:selectedHighlighted forState:UIControlStateSelected | UIControlStateHighlighted];

也就是說按鈕其實有兩類,四種狀態(tài):
1、在selected == NO的時候,有兩種分別對應(yīng) UIControlStateNormal 和 UIControlStateHighlighted
2、在selected == YES的時候,有兩種分別對應(yīng) UIControlStateSelected 和 UIControlStateSelected | UIControlStateHighlighted

由以上的說明,出現(xiàn)"按鈕在selected為YES時,再次點擊按鈕的狀態(tài)變成了normal狀態(tài)"情況的原因:
沒有設(shè)置 UIControlStateSelected | UIControlStateHighlighted 的狀態(tài),系統(tǒng)默認會顯示 UIControlStateNormal 的狀態(tài)

所以需要添加這個狀態(tài)的設(shè)置:

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(50, 100, 100, 40);
button.backgroundColor = [UIColor blackColor];
[button setTitle:@"normal" forState:UIControlStateNormal];
[button setTitle:@"selected" forState:UIControlStateSelected];
// 添加此狀態(tài)
[button setTitle:@"selected" forState:UIControlStateSelected | UIControlStateHighlighted];
button.selected = YES;
[self.view addSubview:button];

最后:

支持這種寫法的方法 setXXX: forState:

- (void)setTitle:(nullable NSString *)title forState:(UIControlState)state;                     // default is nil. title is assumed to be single line
- (void)setTitleColor:(nullable UIColor *)color forState:(UIControlState)state UI_APPEARANCE_SELECTOR; // default if nil. use opaque white
- (void)setTitleShadowColor:(nullable UIColor *)color forState:(UIControlState)state UI_APPEARANCE_SELECTOR; // default is nil. use 50% black
- (void)setImage:(nullable UIImage *)image forState:(UIControlState)state;                      // default is nil. should be same size if different for different states
- (void)setBackgroundImage:(nullable UIImage *)image forState:(UIControlState)state UI_APPEARANCE_SELECTOR; // default is nil
- (void)setAttributedTitle:(nullable NSAttributedString *)title forState:(UIControlState)state NS_AVAILABLE_IOS(6_0); // default is nil. title is assumed to be single line

原文鏈接:iOS系統(tǒng)控件UIButton的狀態(tài)--你不知道的小秘密

位移位運算:
如 UIViewAutoresizingFlexibleHeight = 1 << 4,

1.左移運算 1 << 4
將一個運算對象的各二進制位全部左移若干位(左邊的二進制位丟棄,右邊補0)。

1 轉(zhuǎn)化為二進制為 :0000 0001

左移四位就為 :0001 0000

0001 0000 轉(zhuǎn)化為十進制等于16

·

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

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

  • 說一下相關(guān)的問題:有的時候我們需要設(shè)置按鈕的各個狀態(tài),一般比較常見的就是設(shè)置三種狀態(tài):UIControlState...
    踩坑小分隊閱讀 5,482評論 8 13
  • Button在使用過程中都有哪些常見的狀態(tài)? UIButton繼承自UIControl,有一個枚舉類型的state...
    dominghao閱讀 1,673評論 0 1
  • 一、UIButton的定義 兩種創(chuàng)建方法 1)常規(guī)的initWithFrame的方式 UIButton *b...
    西蜀閱讀 7,096評論 0 1
  • 一、簡介 <<UIButton(按鈕) : 既能顯示文字,又能顯示圖片,還能隨時調(diào)整內(nèi)部圖片和文字的位置,實現(xiàn)了監(jiān)...
    無邪8閱讀 5,778評論 0 2
  • 一顆空空無力的心,漫無目的的飄蕩在心靈構(gòu)建的荒野里落寞寂寥。自從流產(chǎn)后整個人都不再是原來的樣子,恍若自己的心也隨著...
    譯嫻閱讀 322評論 0 2

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