iPhone CGContext: drawing two lines with two different colors

Posted by phonecoddy on Stack Overflow See other posts from Stack Overflow or by phonecoddy
Published on 2010-06-15T00:08:14Z Indexed on 2010/06/15 0:12 UTC
Read the original article Hit count: 276

Filed under:
|
|
|

I am having some troubles using the CGContext with an iPhone app. I am trying to draw several lines with different colors, but all the lines always end up having to color, which was used last. I tried several approaches I could think of, but haven't been lucky.

I set up a small sample project to deal with that issue. This is my code, I use in the drawRect method. I am trying to draw a red and a blue line:

- (void)drawRect:(CGRect)rect{
    NSLog(@"drawrect!");
    CGContextRef bluecontext = UIGraphicsGetCurrentContext(); 
    CGContextSetLineWidth(bluecontext, 2.0);
    CGContextSetStrokeColorWithColor(bluecontext, [UIColor blueColor].CGColor);
    CGContextMoveToPoint(bluecontext, 1, 1);
    CGContextAddLineToPoint(bluecontext, 100, 100);
    CGContextSetStrokeColorWithColor(bluecontext, [UIColor redColor].CGColor);
    CGContextAddLineToPoint(bluecontext, 200, 100);
    CGContextStrokePath(bluecontext);
}

thanks for your help

© Stack Overflow or respective owner

Related posts about iphone

Related posts about line