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

在类似点赞或切换浏览模式等功能的时候,需要用到button的选中状态:即点击后按钮切换图片,并保持这个状态,直到下一次点击.
这里写图片描述
这样设置后,按钮从normal变为selected的过程看起来似乎行得通了,但是,从selected再变回normal的过程还是会出现那个该死的hightLighted状态.
这里写图片描述
感到奇怪吧?我们明明已经设置了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];
        }
    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值