0

我正在使用下面的代码来显示一个音频可视化器,它基本上显示了 2 个条,一个用于左输入,一个用于右输入,这些工作正常,但我想知道是否有任何方法可以更改矩形的颜色,所以只是例如,峰值功率将为红色,而正常值仍为绿色条?这是我的代码。

    - (void)drawRect:(CGRect)rect {

CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [UIColor greenColor].CGColor);
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
CGContextFillRect(context, CGRectMake(15.0, 10.0, kMeterViewFullWidth * leftValue, 15.0));
CGContextFillRect(context, CGRectMake(15.0, 35.0, kMeterViewFullWidth * rightValue, 15.0));
} else {
    CGContextFillRect(context, CGRectMake(15.0, 10.0, kMeterViewFullWidthiPad * leftValue, 15.0));
    CGContextFillRect(context, CGRectMake(15.0, 35.0, kMeterViewFullWidthiPad * rightValue, 15.0));
}
CGContextFlush(context);
}
4

1 回答 1

1

我已经设法使用以下代码完成了我需要的事情,有很多 if 语句,但它有效,希望这将有助于其他需要它的人

    - (void)drawRect:(CGRect)rect {
// Drawing code
[[UIColor greenColor] set];
[@"L" drawInRect:CGRectMake(0.0, 10.0, 10.0, 15.0) withFont:[UIFont boldSystemFontOfSize:[UIFont systemFontSize]] lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentCenter];
[@"R" drawInRect:CGRectMake(0.0, 35.0, 10.0, 15.0) withFont:[UIFont boldSystemFontOfSize:[UIFont systemFontSize]] lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentCenter];

CGRect tempLeftRect = CGRectMake(15, 10, kMeterViewFullWidth * leftValue, 15.0);
NSLog(@"%f", tempLeftRect.size.width);
CGRect tempRightRect = CGRectMake(15, 10, kMeterViewFullWidth * leftValue, 15.0);
NSLog(@"%f", tempRightRect.size.width);

if (tempLeftRect.size.width > 0) {
    [self firstLeftBar];
}
if (tempLeftRect.size.width > 27) {
    [self secondLeftBar];
}
if (tempLeftRect.size.width > 39) {
    [self thirdLeftBar];
}
if (tempLeftRect.size.width > 51) {
    [self forthLeftBar];
}
if (tempLeftRect.size.width > 63) {
    [self fitfhLeftBar];
}
if (tempLeftRect.size.width > 75) {
    [self sixthLeftBar];
}
if (tempLeftRect.size.width > 87) {
    [self seventhLeftBar];
}
if (tempLeftRect.size.width > 99) {
    [self eigthLeftBar];
}
if (tempLeftRect.size.width > 111) {
    [self ninthLeftBar];
}
if (tempLeftRect.size.width > 123) {
    [self tenthLeftBar];
}
if (tempLeftRect.size.width > 135) {
    [self eleventhLeftBar];
}
if (tempLeftRect.size.width > 147) {
    [self twelthLeftBar];
}
if (tempLeftRect.size.width > 159) {
    [self thirteenthLeftBar];
}
if (tempLeftRect.size.width > 171) {
    [self forteenthLeftBar];
}
if (tempLeftRect.size.width > 183) {
    [self fitfhteenthLeftBar];
}
if (tempLeftRect.size.width > 195) {
    [self sixteenthLeftBar];
}
if (tempLeftRect.size.width > 207) {
    [self seventeenthLeftBar];
}
if (tempLeftRect.size.width > 219) {
    [self eighteenthLeftBar];
}
if (tempLeftRect.size.width > 231) {
    [self nineteenthLeftBar];
}
if (tempLeftRect.size.width > 243) {
    [self twentyethLeftBar];
}

if (tempRightRect.size.width > 0) {
    [self firstRightBar];
}
if (tempRightRect.size.width > 27) {
    [self secondRightBar];
}
if (tempRightRect.size.width > 39) {
    [self thirdRightBar];
}
if (tempRightRect.size.width > 51) {
    [self forthRightBar];
}
if (tempRightRect.size.width > 63) {
    [self fitfhRightBar];
}
if (tempRightRect.size.width > 75) {
    [self sixthRightBar];
}
if (tempRightRect.size.width > 87) {
    [self seventhRightBar];
}
if (tempRightRect.size.width > 99) {
    [self eigthRightBar];
}
if (tempRightRect.size.width > 111) {
    [self ninthRightBar];
}
if (tempRightRect.size.width > 123) {
    [self tenthRightBar];
}
if (tempRightRect.size.width > 135) {
    [self eleventhRightBar];
}
if (tempRightRect.size.width > 147) {
    [self twelthRightBar];
}
if (tempRightRect.size.width > 159) {
    [self thirteenthRightBar];
}
if (tempRightRect.size.width > 171) {
    [self forteenthRightBar];
}
if (tempRightRect.size.width > 183) {
    [self fitfhteenthRightBar];
}
if (tempRightRect.size.width > 195) {
    [self sixteenthRightBar];
}
if (tempRightRect.size.width > 207) {
    [self seventeenthRightBar];
}
if (tempRightRect.size.width > 219) {
    [self eighteenthRightBar];
}
if (tempRightRect.size.width > 231) {
    [self nineteenthRightBar];
}
if (tempRightRect.size.width > 243) {
    [self twentyethRightBar];
}



/*
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [UIColor greenColor].CGColor);
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
CGContextFillRect(context, CGRectMake(15.0, 10.0, kMeterViewFullWidth * leftValue, 15.0));
CGContextFillRect(context, CGRectMake(15.0, 35.0, kMeterViewFullWidth * rightValue, 15.0));
} else {
    CGContextFillRect(context, CGRectMake(15.0, 10.0, kMeterViewFullWidthiPad * leftValue, 15.0));
    CGContextFillRect(context, CGRectMake(15.0, 35.0, kMeterViewFullWidthiPad * rightValue, 15.0));
}
CGContextFlush(context);
 */
}


- (void)updateMeterWithLeftValue:(CGFloat)left rightValue:(CGFloat)right {
leftValue = left;
rightValue = right;
[self setNeedsDisplay];
}

-(void)firstLeftBar{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [UIColor greenColor].CGColor);
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
    CGContextFillRect(context, CGRectMake(15.0, 10.0, 9.0, 15.0));
}
CGContextFlush(context);
}

-(void)secondLeftBar{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [UIColor greenColor].CGColor);
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
    CGContextFillRect(context, CGRectMake(27.0, 10.0, 9.0, 15.0));
}
CGContextFlush(context);
}

-(void)thirdLeftBar{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [UIColor greenColor].CGColor);
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
    CGContextFillRect(context, CGRectMake(39.0, 10.0, 9.0, 15.0));
}
CGContextFlush(context);
}

-(void)forthLeftBar{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [UIColor greenColor].CGColor);
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
    CGContextFillRect(context, CGRectMake(51.0, 10.0, 9.0, 15.0));
}
CGContextFlush(context);
}

-(void)fitfhLeftBar{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [UIColor greenColor].CGColor);
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
    CGContextFillRect(context, CGRectMake(63.0, 10.0, 9.0, 15.0));
}
CGContextFlush(context);
}

-(void)sixthLeftBar{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [UIColor greenColor].CGColor);
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
    CGContextFillRect(context, CGRectMake(75.0, 10.0, 9.0, 15.0));
}
CGContextFlush(context);
}

-(void)seventhLeftBar{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [UIColor greenColor].CGColor);
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
    CGContextFillRect(context, CGRectMake(87.0, 10.0, 9.0, 15.0));
}
CGContextFlush(context);
}

-(void)eigthLeftBar{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [UIColor greenColor].CGColor);
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
    CGContextFillRect(context, CGRectMake(99.0, 10.0, 9.0, 15.0));
}
CGContextFlush(context);
}

-(void)ninthLeftBar{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [UIColor greenColor].CGColor);
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
    CGContextFillRect(context, CGRectMake(111.0, 10.0, 9.0, 15.0));
}
CGContextFlush(context);
}

-(void)tenthLeftBar{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [UIColor greenColor].CGColor);
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
    CGContextFillRect(context, CGRectMake(123.0, 10.0, 9.0, 15.0));
}
CGContextFlush(context);
}

-(void)eleventhLeftBar{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [UIColor greenColor].CGColor);
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
    CGContextFillRect(context, CGRectMake(135.0, 10.0, 9.0, 15.0));
}
CGContextFlush(context);
}

-(void)twelthLeftBar{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [UIColor greenColor].CGColor);
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
    CGContextFillRect(context, CGRectMake(147.0, 10.0, 9.0, 15.0));
}
CGContextFlush(context);
}

-(void)thirteenthLeftBar{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [UIColor greenColor].CGColor);
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
    CGContextFillRect(context, CGRectMake(159.0, 10.0, 9.0, 15.0));
}
CGContextFlush(context);
}

-(void)forteenthLeftBar{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [UIColor greenColor].CGColor);
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
    CGContextFillRect(context, CGRectMake(171.0, 10.0, 9.0, 15.0));
}
CGContextFlush(context);
}

-(void)fitfhteenthLeftBar{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [UIColor greenColor].CGColor);
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
    CGContextFillRect(context, CGRectMake(183.0, 10.0, 9.0, 15.0));
}
CGContextFlush(context);
}

-(void)sixteenthLeftBar{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
    CGContextFillRect(context, CGRectMake(195.0, 10.0, 9.0, 15.0));
}
CGContextFlush(context);
}

-(void)seventeenthLeftBar{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
    CGContextFillRect(context, CGRectMake(207.0, 10.0, 9.0, 15.0));
}
CGContextFlush(context);
}

-(void)eighteenthLeftBar{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
    CGContextFillRect(context, CGRectMake(219.0, 10.0, 9.0, 15.0));
}
CGContextFlush(context);
}

-(void)nineteenthLeftBar{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
    CGContextFillRect(context, CGRectMake(231.0, 10.0, 9.0, 15.0));
}
CGContextFlush(context);
}

-(void)twentyethLeftBar{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
    CGContextFillRect(context, CGRectMake(243.0, 10.0, 9.0, 15.0));
}
CGContextFlush(context);
}

-(void)firstRightBar{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [UIColor greenColor].CGColor);
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
    CGContextFillRect(context, CGRectMake(15.0, 35.0, 9.0, 15.0));
}
CGContextFlush(context);
}

-(void)secondRightBar{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [UIColor greenColor].CGColor);
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
    CGContextFillRect(context, CGRectMake(27.0, 35.0, 9.0, 15.0));
}
CGContextFlush(context);
}

-(void)thirdRightBar{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [UIColor greenColor].CGColor);
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
    CGContextFillRect(context, CGRectMake(39.0, 35.0, 9.0, 15.0));
}
CGContextFlush(context);
}

-(void)forthRightBar{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [UIColor greenColor].CGColor);
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
    CGContextFillRect(context, CGRectMake(51.0, 35.0, 9.0, 15.0));
}
CGContextFlush(context);
}

-(void)fitfhRightBar{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [UIColor greenColor].CGColor);
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
    CGContextFillRect(context, CGRectMake(63.0, 35.0, 9.0, 15.0));
}
CGContextFlush(context);
}

-(void)sixthRightBar{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [UIColor greenColor].CGColor);
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
    CGContextFillRect(context, CGRectMake(75.0, 35.0, 9.0, 15.0));
}
CGContextFlush(context);
}

-(void)seventhRightBar{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [UIColor greenColor].CGColor);
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
    CGContextFillRect(context, CGRectMake(87.0, 35.0, 9.0, 15.0));
}
CGContextFlush(context);
}

-(void)eigthRightBar{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [UIColor greenColor].CGColor);
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
    CGContextFillRect(context, CGRectMake(99.0, 35.0, 9.0, 15.0));
}
CGContextFlush(context);
}

-(void)ninthRightBar{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [UIColor greenColor].CGColor);
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
    CGContextFillRect(context, CGRectMake(111.0, 35.0, 9.0, 15.0));
}
CGContextFlush(context);
}

-(void)tenthRightBar{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [UIColor greenColor].CGColor);
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
    CGContextFillRect(context, CGRectMake(123.0, 35.0, 9.0, 15.0));
}
CGContextFlush(context);
}

-(void)eleventhRightBar{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [UIColor greenColor].CGColor);
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
    CGContextFillRect(context, CGRectMake(135.0, 35.0, 9.0, 15.0));
}
CGContextFlush(context);
}

-(void)twelthRightBar{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [UIColor greenColor].CGColor);
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
    CGContextFillRect(context, CGRectMake(147.0, 35.0, 9.0, 15.0));
}
CGContextFlush(context);
}

-(void)thirteenthRightBar{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [UIColor greenColor].CGColor);
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
    CGContextFillRect(context, CGRectMake(159.0, 35.0, 9.0, 15.0));
}
CGContextFlush(context);
}

-(void)forteenthRightBar{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [UIColor greenColor].CGColor);
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
    CGContextFillRect(context, CGRectMake(171.0, 35.0, 9.0, 15.0));
}
CGContextFlush(context);
}

-(void)fitfhteenthRightBar{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [UIColor greenColor].CGColor);
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
    CGContextFillRect(context, CGRectMake(183.0, 35.0, 9.0, 15.0));
}
CGContextFlush(context);
}

-(void)sixteenthRightBar{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
    CGContextFillRect(context, CGRectMake(195.0, 35.0, 9.0, 15.0));
}
CGContextFlush(context);
}

-(void)seventeenthRightBar{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
    CGContextFillRect(context, CGRectMake(207.0, 35.0, 9.0, 15.0));
}
CGContextFlush(context);
}

-(void)eighteenthRightBar{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
    CGContextFillRect(context, CGRectMake(219.0, 35.0, 9.0, 15.0));
}
CGContextFlush(context);
}

-(void)nineteenthRightBar{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
    CGContextFillRect(context, CGRectMake(231.0, 35.0, 9.0, 15.0));
}
CGContextFlush(context);
}

-(void)twentyethRightBar{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
    CGContextFillRect(context, CGRectMake(243.0, 35.0, 9.0, 15.0));
}
CGContextFlush(context);
}
于 2013-07-20T10:52:37.550 回答