1

我想知道是否有人可以帮助我在我的 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”

谢谢你的帮助!

-卡尔

4

2 回答 2

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 );
于 2012-06-21T16:58:07.397 回答
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);
...
}
于 2012-06-19T18:59:07.430 回答