设置按钮选中有点击效果 类似cell 的选中效果

按钮添加事件:

[button1 addTarget:self action:@selector(button1Selected:) forControlEvents:UIControlEventTouchUpInside];

对应的事件:

// button选中的颜色
- (void)button1Selected:(UIButton *)sender
{

  //设置按钮选中的背景颜色
    sender.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5]

    [self performSelector:@selector(btnClearColor:) withObject:sender afterDelay:0.3];
   

}

// button清除选中色
- (void)btnClearColor:(UIButton *)sender
{
设置选中的背景颜色灰色颜色
sender.backgroundColor = [UIColor clearColor] ;

}

注:

  1. colorWithAlphaComponent 使用该方法可以避免设置按扭透明度的时候,文字也跟着有一定的透明度的情况。
  2. 在清楚背景色的时候 一定要带object:btn 过去否则 在清楚选中色的时候找不到对应的btn

你可能感兴趣的:(设置按钮选中有点击效果 类似cell 的选中效果)