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

    - (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);
}

最佳答案

我已经使用以下代码设法完成了我需要的工作,有很多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);
}

关于ios - 根据大小更改CGContextFillRect的颜色,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17710631/

10-11 19:47