2013-03-23 64 views
0

我正在繪製一個圖形,我想上升線爲綠色,下降線爲紅色。我嘗試以下內容:使用多種顏色撫摸上下文

CGContextRef context = UIGraphicsGetCurrentContext(); 

CGContextSetLineWidth(context, 2); 
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor); 

CGPoint previousPoint = CGPointMake(0.0, 0.0); 

//graph the points 
for (int i = 0; i < self.numberOfS; i++) { 
    CGPoint currentPoint = CGPointMake(150+(40*[[[self.grades objectAtIndex:i] s] doubleValue]), 59+(40*(i+1))); 
    if (previousPoint.x != 0.0f) { 
     if (previousPoint.x > currentPoint.x) { 
      CGContextMoveToPoint(context, previousPoint.x, previousPoint.y); 
      CGContextSetStrokeColorWithColor(context, [UIColor greenColor].CGColor); 
      CGContextAddLineToPoint(context, 150+(40*[[[self.grades objectAtIndex:i] semesterGPA] doubleValue]), 59+(40*(i+1))); 
     } else{ 
      CGContextMoveToPoint(context, previousPoint.x, previousPoint.y); 
      CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor); 
      CGContextAddLineToPoint(context, 150+(40*[[[self.grades objectAtIndex:i] semesterGPA] doubleValue]), 59+(40*(i+1))); 
     } 

    } 
    previousPoint.x = currentPoint.x; 
    previousPoint.y = currentPoint.y; 
} 

但它不工作,一切都是紅色的。我怎樣才能解決這個問題 ?

+0

你在哪裏其實** **繪製的路徑?我只看到你創造它。 – borrrden 2013-03-23 15:09:06

+0

也是,你正在檢查前面的x是否大於當前的x,這永遠不會發生?你不應該檢查y嗎? – 2013-03-23 15:10:33

+0

圖形都很好,但線條是紅色的... CGContextSetStrokeColorWithColor爲綠色不起作用。這就像只輸入第一個(第三行代碼)。我在UIView的drawRect方法中做了所有的事情。 @maxk。我將圖形繪製爲橫向模式,但視圖座標未移位,因此我繼續使用舊座標進行繪製,以便我可以在橫向上查看它。 – HusseinB 2013-03-23 15:24:59

回答

0

好的解決了這個問題,我剛進入這行代碼:

CGContextStrokePath(context); 
+0

正確,在設置新的上下文設置後,您需要對其進行描邊。 – 2013-03-23 17:38:27