在iPad绘画应用程序中设置颜色

时间:2012-06-18 16:42:33

标签: objective-c ios xcode colors paint

我想知道是否有人可以帮助我在我的iOS应用中实现颜色选择。现在,画线只是黑色,但我希望用户能够改变颜色。现在我有5个按钮,可以选择5种颜色。然后我标记了每个按钮1-5,并将它们全部连接到一个IBAction。我以为我可以有一个if语句来说明按钮是否标记为1,然后将颜色设置为1.0,0.0,0.0,1.0(红色)等等。  所以,这是我的if语句:

NSString *color = @"0.15, 1.15, 0.15, .8";

-(IBAction)color:(id)sender{

    if ([sender tag] ==1) {
        color = @"0.15, 1.15, 0.15, .8";
    }
    if ([sender tag] ==2) {
        color = @"0.15, 1.15, 0.15, .8";
    }
    if ([sender tag] ==3) {
        color = @"0.15, 1.15, 0.15, .8);";
    }
    if ([sender tag] ==4) {
        color = @"(0.15, 1.15, 0.15, .8";
    }
    if ([sender tag] ==5) {
        color = @"0.15, 1.15, 0.15, .8";
    }
}

所以,现在我把变量颜色放在

CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), color );

但是,我收到错误“函数调用参数太少,预期5,有2”

感谢您的帮助!

-Karl

2 个答案:

答案 0 :(得分:1)

感谢您的帮助,但我自己想出了最后一部分。 如果有人感兴趣,这就是我提出的代码:

double C1 = 1.15;
double C2 = 0.15;
double C3 = 0.15;
double C4 = .8;

'NSString *color = @"0.15, 1.15, 0.15, .8";

-(IBAction)colors:(id)sender{

if (([sender tag] ==1)) {
    C1=  0.15;
    C2 = 0.15;
    C3 = 0.15;
    C4 = .8;
}

else if (([sender tag] ==2)) {

    C1 = 0.15;
    C2 = 1.15;
    C3 = 0.15;
    C4 = .8;
}

else if (([sender tag] ==3)) {
    color = @"1.15, 0.15, 0.15, .8";
    C1 = 1.15;
    C2 = 0.15;
    C3 = 0.15;
    C4 = .8;
}

else if (([sender tag] ==4)) {
    C1 = 0.15;
    C2 = 0.15;
    C3 = 1.15;
    C4 = .8;
}
else if (([sender tag] ==5)) {
    C1 = 1.15;
    C2 = 1.15;
    C3 = 0.15;
    C4 = .8;
}

}

CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), C1, C2, C3, C4 );

答案 1 :(得分:0)

/*
[colorArray addObject:@"0.0,0.0,0.0"];      
[colorArray addObject:@"148.0,0.0,211.0"];  
[colorArray addObject:@"0.0,191.0,255.0"];  
...
*/

-(IBAction)actionColorButton:(id)sender
{
[self setPenColor:[colorArray objectAtIndex:[sender tag] - 1]];
}

-(void)setPenColor:(NSString*)color
{
NSArray *arr = [color componentsSeparatedByString:@","];

colorRed = [[arr objectAtIndex:0] floatValue] / 255.0;
colorGreen = [[arr objectAtIndex:1] floatValue] / 255.0;
colorBlue = [[arr objectAtIndex:2] floatValue] / 255.0;
...
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 
                           colorRed,
                           colorGreen,
                           colorBlue,
                           1.0);
...
}