ios - Changing colour of CGContextFillRect depending on size -


i using following code show audio visualizer, shows 2 bars 1 left input , 1 right input, these work fine want know if there way change colour of rect peak power red instance while normal value still green bar? here code.

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

i have managed needed using following code, there lot of if statements works, hope else needs it

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

Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -