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;
}
但它不工作,一切都是紅色的。我怎樣才能解決這個問題 ?
你在哪裏其實** **繪製的路徑?我只看到你創造它。 – borrrden 2013-03-23 15:09:06
也是,你正在檢查前面的x是否大於當前的x,這永遠不會發生?你不應該檢查y嗎? – 2013-03-23 15:10:33
圖形都很好,但線條是紅色的... CGContextSetStrokeColorWithColor爲綠色不起作用。這就像只輸入第一個(第三行代碼)。我在UIView的drawRect方法中做了所有的事情。 @maxk。我將圖形繪製爲橫向模式,但視圖座標未移位,因此我繼續使用舊座標進行繪製,以便我可以在橫向上查看它。 – HusseinB 2013-03-23 15:24:59