关于UIButton如何点击后改变状态+圆角按钮组合拳

示例代码

    button.titleLabel.textAlignment = NSTextAlignmentCenter;     //一般圆角按钮文字肯定居中
    button.layer.masksToBounds = YES;   //设定这个才有圆角
    button.layer.cornerRadius = 3;  //设定弯曲度
    [button setTitle:@"25" forState:UIControlStateNormal];
    [button setTitle:@"26" forState:UIControlStateSelected];
    [button addTarget:self action:@selector(touchZan:) forControlEvents:UIControlEventTouchUpInside];

    - (void)touchZan:(UIButton*)button {
    button.selected = !button.selected;
}

讲解

  1. 首先要说明的是一点,UIButton的selected属性必须要为设置才能生效,所以要添加一个点击事件,加入button.selected = !button.selected;
  2. 另外对于状态的设定直接放在上面就好,不要放在点击事件里,可能会导致需要点击两次才会改变的情况

猜你喜欢

转载自blog.csdn.net/KevinAshen/article/details/81390687