ios - 根据大小更改 CGContextFillRect 的颜色

标签 ios audio cgcontext visualizer

我使用以下代码来显示音频可视化工具,它基本上显示 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);
}

最佳答案

我已经成功地使用以下代码完成了我需要的操作,有很多 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/

相关文章:

iphone - 推送或弹出导航 Controller 时是否需要调整 UITableView 的大小 - IOS?

python - 在 Windows(和 Linux)上播放各种音频的最佳 python 解决方案?

ios - CGBitmapContextCreate在iPhone 6+中提供EXC_BAD_ACCESS

audio - 如何使用C分析音乐?

android - 读取的AudioRecord返回奇怪的值

ios - 用 UIGraphicsImageRenderer 的 jpegData 替换 UIImageJPEGRepresentation

ios - CGContext 初始化后不绘制

ios - 以编程方式获取导航栏的高度

ios - 处理 CAKeyFrameAnimation 的正确方法

ios - 在 macOS Sierra 10.12 上运行 Xcode 6.x