按钮单击时颜色更改

时间:2012-11-15 06:18:59

标签: iphone ios uibutton uicolor

在我的签名视图中,我想提供更改Ibaction颜色的选项。例如,如果用户单击红色按钮,绘图颜色应为红色,就像那样,我尝试使用教程http://www.edumobile.org/iphone/iphone-beginner-tutorials/digital-signature-application-in-iphone/,这里是着色代码

 CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0);

我怀疑如何将此作为单独的按钮方法,并在点击时执行颜色?

2 个答案:

答案 0 :(得分:1)

嗨,只需将值作为变量和

如果您在drawrect method

中编写此代码

您可以按[self setNeedsDisplay];

进行更新
- (void)drawRect:(CGRect)rect
{
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), redValue, greenValue, blueValue, alpha);
}

-(IBAction)updateColor{

   redValue = red color code;
   greenValue = green color code;
   blueValue = blue color code;
  alphaValue = alpha value;

   [self setNeedsDisplay];
}

答案 1 :(得分:0)

您只需在红色按钮上单击

即可编写此代码
       [button setBackgroundColor:[UIColor redColor]];