iPhone CGContext: drawing two lines with two different colors

余生长醉 提交于 2019-12-03 02:35:41

Insert this code just before you set the stroke color the second time:

CGContextStrokePath(bluecontext);
CGContextBeginPath(bluecontext);

All the AddLine and AddOther calls are building a path. The path is drawn with a call like StrokePath, using the most recently set colors and other attributes. You are trying to draw two separate paths, so you must call Begin and Stroke for each path. Begin is sort of implicit when you start drawing, although it does not hurt to call it yourself. The basic flow of drawing is:

CGContextBeginPath(bluecontext); // clears any previous path
// add lines, curves, rectangles, etc...
CGContextStrokePath(bluecontext); // renders the path

Thats what you need.

CGContextRef context = UIGraphicsGetCurrentContext();
CGContextClearRect(context, rect);
CGContextSetLineWidth(context, 2.0);

CGContextBeginPath(context);
CGContextSetStrokeColorWithColor(context, [UIColor orangeColor].CGColor);
CGContextMoveToPoint(context, 1, 1);
CGContextAddLineToPoint(context, 100, 100);
CGContextStrokePath(context); // and draw orange line}

CGContextBeginPath(context);
CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
CGContextMoveToPoint(context, 100, 100);
CGContextAddLineToPoint(context, 200, 100);     
CGContextStrokePath(context); // draw blue line

I think this could be working.

CGContextRef bluecontext = UIGraphicsGetCurrentContext(); 
CGContextSetLineWidth(bluecontext, 2.0);
CGContextSetStrokeColorWithColor(bluecontext, [UIColor blueColor].CGColor);
CGContextMoveToPoint(bluecontext, 1, 1);
CGContextAddLineToPoint(bluecontext, 100, 100);

CGContextStrokePath(bluecontext); // draw blue line


CGContextSetStrokeColorWithColor(bluecontext, [UIColor redColor].CGColor);
CGContextAddLineToPoint(bluecontext, 200, 100);

CGContextStrokePath(bluecontext); // and draw red line

If you are interested in the way it looks in a loop:

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

    CGPoint startingPoint = [[pointsArray objectAtIndex:0] CGPointValue];
    CGContextMoveToPoint(context, startingPoint.x, startingPoint.y); //start at this point

    for (int i = 1; i < [pointsArray count]; i++) {
        CGContextBeginPath(context);
        //start at the previous point
        CGContextMoveToPoint(context, 
               [[pointsArray objectAtIndex:i-1] CGPointValue].x, 
               [[pointsArray objectAtIndex:i-1] CGPointValue].y);

        CGPoint point = [[pointsArray objectAtIndex:i] CGPointValue];
        if (point.y < 50) { // if y is less then 50 use red color
            CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
        } else { // else use blue color
            CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);

        }
        CGContextAddLineToPoint(context, point.x, point.y); //draw to this point
        CGContextStrokePath(context);
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!