iOS中UIButton的多种状态的隐藏设置

在类似点赞或切换浏览模式等功能的时候,需要用到button的选中状态:即点击后按钮切换图片,并保持这个状态,直到下一次点击.
iOS中UIButton的多种状态的隐藏设置_第1张图片
这样设置后,按钮从normal变为selected的过程看起来似乎行得通了,但是,从selected再变回normal的过程还是会出现那个该死的hightLighted状态.
iOS中UIButton的多种状态的隐藏设置_第2张图片
感到奇怪吧?我们明明已经设置了hightLighted状态下的图片,怎么回来的路行不通呢? 有没有可能从selected状态变回normal状态这个过程经历的并不是hightLighted状态,而是其他什么状态呢?

但是使用过这种方法的人应该都会遇到这样一个问题:不管按钮从normal状态转为selected状态,还是反过来,中间都会经历一个highLighted状态,这就导致在状态切换的过程中有一次图片的跳变.如图:

这样设置后,按钮从normal变为selected的过程看起来似乎行得通了,但是,从selected再变回normal的过程还是会出现那个该死的hightLighted状态.

感到奇怪吧?我们明明已经设置了hightLighted状态下的图片,怎么回来的路行不通呢? 有没有可能从selected状态变回normal状态这个过程经历的并不是hightLighted状态,而是其他什么状态呢?

typedef NS_OPTIONS(NSUInteger, UIControlState) {
    UIControlStateNormal       = 0,       // 0000
    UIControlStateHighlighted  = 1 << 0,  // 0001
    UIControlStateDisabled     = 1 << 1,  // 0010
    UIControlStateSelected     = 1 << 2,  // 0100 
    UIControlStateFocused      = 1 << 3,  // 1000
    UIControlStateApplication  = 0x00FF0000,       
    UIControlStateReserved     = 0xFF000000  
};
就是这几种情况,其实不然,它们的状态就可以多种组合的,会出现很多其他的状态,这也是为啥苹果要用 1 << 0 的方式去枚举按钮状态的原因吧
如:
// 0101 此为 选中态的 点击状态  (只有在这种情况下设置才会在 选中态 点击按钮才能设成你想要的 样子)
UIControlStateHighlighted | UIControlStateSelected

// 0110 此为 选中态的 不可点击状态
UIControlStateDisabled | UIControlStateSelected

0000, 每一位数都有各自的意思,我们将这样的

{
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
// 未选中态
[button setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor] cornerRadius:5] forState:UIControlStateNormal];//
[button setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];

        // 选中态
        [button setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor] cornerRadius:5] forState:UIControlStateSelected]; // 态
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateSelected];

        { //未选中时的点击态
            [button setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor] cornerRadius:5] forState:UIControlStateDisabled];
            [button setTitleColor:[UIColor colorWithWhite:0.8 alpha:1] forState:UIControlStateDisabled];

            // 选中时的不可点击状态
            [button setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor] cornerRadius:5] forState:UIControlStateDisabled | UIControlStateSelected];
            [button setTitleColor:[UIColor colorWithWhite:0.8 alpha:1] forState:UIControlStateDisabled | UIControlStateSelected];
        }

        { // 未选中时的点击态
            [button setBackgroundImage:[UIImage imageWithColor:[UIColor orangeColor] cornerRadius:5] forState:UIControlStateHighlighted]; // 点击态
            [button setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];

            //  选中时的点击态
            [button setBackgroundImage:[UIImage imageWithColor:[UIColor orangeColor] cornerRadius:5] forState:UIControlStateHighlighted | UIControlStateSelected]; 
            [button setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted | UIControlStateSelected];
        }
    }

你可能感兴趣的:(UserInterface,UI,阶段)