【问题标题】:Setting color in an iPad painting app在 iPad 绘画应用程序中设置颜色
【发布时间】:2012-06-18 16:42:32
【问题描述】:

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

感谢您的帮助!

-卡尔

【问题讨论】:

  • 首先,您不应该将变量声明 5 次。放 'NSString *color;'在 if 语句上方,然后只说 'color = @"whatever color you want";'在 if 语句中。
  • 非常感谢!这有很大帮助。现在看起来很明显,我想我已经有一段时间没有编程了。哈哈
  • 我想我不必说你应该在谷歌上搜索“范围”,但这让其他有同样问题的人可以理解。
  • 现在解决了吗?如果是,我会发布一个答案,以便您接受。
  • 另外,你有什么理由使用 NSString 而不是 UIColor?

标签: objective-c ios xcode colors paint


【解决方案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 );

【讨论】:

    【解决方案2】:
    /*
    [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);
    ...
    }
    

    【讨论】:

      猜你喜欢
      • 2013-03-12
      • 2014-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-11
      相关资源
      最近更新 更多