它不会改变线条的颜色

时间:2014-12-24 04:15:32

标签: ios objective-c iphone cocoa-touch

这是Objective-C代码,我正在尝试更改行的颜色,但它不会反映在代码上:(

   for (int i = 1; i < count-1; i++){
    value = [[self.arrayOfGraphData objectAtIndex:i] floatValue];

    if (value <= 0.4) {
        CGContextSetStrokeColorWithColor(ctx, [[UIColor![enter image description here][1] redColor] CGColor]);
    }
    else if(value > 0.4 && value <= 0.7){
        CGContextSetStrokeColorWithColor(ctx, [[UIColor orangeColor] CGColor]);
    }
    else{
        CGContextSetStrokeColorWithColor(ctx, [[UIColor purpleColor] CGColor]);
    }

    CGContextMoveToPoint(ctx, firstPointX, firstPointY);

    float xCoord  = kOffsetX + i * kStepX;
    float yCoord  = kGraphHeight - maxGraphHeight * value;

    CGContextAddLineToPoint(ctx, xCoord, yCoord);

    xCoord  = kOffsetX + (i+1) * kStepX;
    CGContextAddLineToPoint(ctx, xCoord, yCoord);

    firstPointX = xCoord;
    firstPointY = yCoord;
}

CGContextDrawPath(ctx, kCGPathStroke);

以上代码仅显示红色线

1 个答案:

答案 0 :(得分:0)

您选择的颜色仅在您致电CGContextDrawPath时使用。因此,如果您想绘制不同颜色的线条,则需要为每条不同颜色的线条调用CGContextDrawPath。这是一个使用四种颜色绘制正方形的示例。

- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth( context, 10 );
    CGContextSetLineCap( context, kCGLineCapButt );

    CGContextMoveToPoint( context, 5, 10 );
    CGContextAddLineToPoint( context, 95, 10 );
    [[UIColor redColor]setStroke];
    CGContextStrokePath( context );

    CGContextMoveToPoint( context, 90, 15 );
    CGContextAddLineToPoint( context, 90, 85 );
    [[UIColor greenColor]setStroke];
    CGContextStrokePath( context );

    CGContextMoveToPoint( context, 95, 90 );
    CGContextAddLineToPoint( context, 5, 90 );
    [[UIColor blueColor]setStroke];
    CGContextStrokePath( context );

    CGContextMoveToPoint( context, 10, 85 );
    CGContextAddLineToPoint( context, 10, 15 );
    [[UIColor purpleColor]setStroke];
    CGContextStrokePath( context );
}