活用UIButton的状态设置

前言:项目中,设置UIButton的颜色,背景来显示不同的状态是很常见的操作,比如:不可点置灰,点击高亮等等。下面,记录一下,在这方面踩的的坑。

1.UIButton可以设置4种状态

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而不是枚举,也就是说,这些状态可以组合,这就可能存在相互影响的问题,这就是UIControlState的大坑。

2.简要介绍一下四种state所代表的意义:

  • UIControlStateNormal

The normal, or default state of a control—that is, enabled but neither selected nor highlighted.
代表enable,unselected,unhighlight的状态,这也是个坑,normal是一切你没有设置的特殊状态都显示normal的状态,不仅仅是这个。

  • 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只和事件有关,比如,当进入点击范围时会高亮,出去点击范围时高亮会消失,但前提下,这个UIButton可点击,否则没有意义。

  • 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.
这个当UIButton不可点击的时候触发

  • UIControlStateSelected

Selected state of a control. For many controls, this state has no effect on behavior or appearance.
UIButton默认selected是NO

3.重点介绍这四种状态的各种显示

UIControlState是BitMask,3 个有效的 Bit 组合其实可以拥有 8 种状态,分别是:

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

//设置UIButton的8种状态
[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];

//设置不同的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];

有图有真相:

活用UIButton的状态设置_第1张图片
UIControlState.png

结果是:
1.UIControlStateHighlighted & UIControlStateDisabled**会覆盖UIControlStateHighlighted的状态 2.UIControlStateSelected & UIControlStateHighlighted & UIControlStateDisabled会覆盖UIControlStateSelected & UIControlStateHighlighted 3.也就说highlight和disable在一起,不管该UIButton是否可点,都是显示highlight和disable

总结:

1.使用UIControlState尽量精确,比如说:可点状态下已选择的高亮
2.注意高亮和不可以点击状态的组合

参考:

1.《UIButton 状态的新理解》
2.apple文档

你可能感兴趣的:(活用UIButton的状态设置)