iOS点击按钮行为

时间:2015-06-25 11:24:03

标签: ios objective-c

我需要在我的应用中使用带边框的iOS UIButton。我用:

btn.layer.borderColor = [UIColor blackColor].CGColor;

btn.layer.borderWidth = 1.0;

btn.layer.cornerRadius = 10;

这样可以正常工作,但点按边框不会闪烁(按钮高亮显示状态)。有什么方法可以轻松实现这个吗?

2 个答案:

答案 0 :(得分:1)

创建按钮时实现此目的:

[self.botonConfigurar addTarget:self action:@selector(setBgColorForButton:) forControlEvents:UIControlEventTouchDown];
[self.botonConfigurar addTarget:self action:@selector(clearBgColorForButton:) forControlEvents:UIControlEventTouchDragExit];
[self.botonConfigurar addTarget:self action:@selector(setBgColorForButton:) forControlEvents:UIControlEventTouchDragEnter];

和这两个功能:

-(void)setBgColorForButton:(UIButton*)sender {

    [self.botonConfigurar setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.botonConfigurar.layer.backgroundColor = [UIColor colorWithRed:89/255.0 green:194/255.0 blue:237/255.0 alpha:1.0].CGColor;
}

-(void)clearBgColorForButton:(UIButton*)sender {

    [self.botonConfigurar setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.botonConfigurar.layer.backgroundColor = [UIColor colorWithRed:61/255.0 green:169/255.0 blue:222/255.0 alpha:1].CGColor;

}

使用这些功能中的按钮做任何你想做的事。

PD:将按钮设为[UIButton buttonWithType:UIButtonTypeCustom]

PD2:在clearBgColorForButton:sender中写下UIButton的原始参数。

答案 1 :(得分:0)

这只是示例

 [button setBackgroundImage:[self imageWithColor:[UIColor blueColor]] forState:UIControlStateHighlighted];