美文网首页
关于UIButton设置selected为YES时,再次长按UI

关于UIButton设置selected为YES时,再次长按UI

作者: 伯牙呀 | 来源:发表于2018-05-04 16:37 被阅读167次

    按钮在 selected 为 YES 时,再次点击或长按,按钮的文字变成 normal 状态的了。

    代码:

    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
    };
    

    按钮的状态是位枚举,不是普通的枚举,也就是说按钮可以有不止一个状态。所以当按钮的selected为YES时,再点击的时候状态应该不是 normal 状态而是其他状态。

    发现了相关的说明: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];
    

    也就是说按钮其实有两类,四种状态:
    1、在selected == NO的时候,有两种分别对应 UIControlStateNormal 和 UIControlStateHighlighted
    2、在selected == YES的时候,有两种分别对应 UIControlStateSelected 和 UIControlStateSelected | UIControlStateHighlighted

    由以上的说明,出现"按钮在selected为YES时,再次点击按钮的状态变成了normal状态"情况的原因:
    没有设置 UIControlStateSelected | UIControlStateHighlighted 的状态,系统默认会显示 UIControlStateNormal 的状态

    所以需要添加这个状态的设置:

    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 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系统控件UIButton的状态--你不知道的小秘密

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

    1.左移运算 1 << 4
    将一个运算对象的各二进制位全部左移若干位(左边的二进制位丢弃,右边补0)。

    1 转化为二进制为 :0000 0001

    左移四位就为 :0001 0000

    0001 0000 转化为十进制等于16

    ·

    相关文章

      网友评论

          本文标题:关于UIButton设置selected为YES时,再次长按UI

          本文链接:https://www.haomeiwen.com/subject/vvbdrftx.html