我正在使用自定义UIButton
,我想要做的就是当用户触摸按钮时它会变暗,或变成浅灰色,就像普通按钮一样。
我只是希望文本暂时改变颜色,当用户抬起时,它会恢复到常规颜色。
我试过这段代码:
button.showsTouchWhenHighlighted = TRUE;
但是这只是在它周围形成一个白圈,这不是我正在寻找的 谢谢你的帮助。
答案 0 :(得分:0)
您可以使用以下UIControlEvents
:
UIControlEventTouchDown
UIControlEventTouchUpInside
UIControlEventTouchUpOutside
将目标操作方法适当地分配给它们:
//when touch initiated
[myButton addTarget:self
action:@selector(buttonTouchStartAct:)
forControlEvents:UIControlEventTouchDown];
//on touch released outside button bounds
[myButton addTarget:self
action:@selector(buttonTouchEndAct:)
forControlEvents:UIControlEventTouchUpOutside];
//on touch released while still inside button bounds (most commonly used)
[myButton addTarget:self
action:@selector(buttonAct:)
forControlEvents:UIControlEventTouchUpInside];
-(void)buttonTouchStartAct:(UIButton *)sender
{
[sender.titleLabel setTextColor:[UIColor redColor]];
}
-(void)buttonTouchEndAct:(UIButton *)sender
{
[sender.titleLabel setTextColor:[UIColor greenColor]];
}
-(void)buttonAct:(UIButton *)sender
{
//touch ended in this case too
[self buttonTouchEndAct:sender];
//...
//your main button logic
//...
}
答案 1 :(得分:0)
//when touch initiated
[myButton addTarget:self
action:@selector(buttonTouchStartAct:)
forControlEvents:UIControlEventTouchDown];
//on touch released outside button bounds
[myButton addTarget:self
action:@selector(buttonTouchEndAct:)
forControlEvents:UIControlEventTouchUpOutside];
//on touch released while still inside button bounds (most commonly used)
[myButton addTarget:self
action:@selector(buttonAct:)
forControlEvents:UIControlEventTouchUpInside];
然后在方法内部更改按钮的颜色
答案 2 :(得分:0)
尝试按钮点击更改标题标签的alpha,如下所示
- (IBAction)buttonClick:(id)sender {
UIButton *button = sender;
[UIView animateWithDuration:0.2 delay:0.0 options:UIViewAnimationOptionAllowUserInteraction|UIViewAnimationOptionCurveEaseInOut animations:^
{
[button.titleLabel setAlpha:0.5];
} completion:^(BOOL finished)
{
[button.titleLabel setAlpha:1];
}];
}