iPhone CGContext:用两种不同颜色绘制两条线
我在使用CGContext和iPhone应用程序时遇到了一些麻烦。我试图用不同的颜色画出几行,但所有的行总是以最后使用的颜色结束。我尝试了几种方法,但没有幸运。iPhone CGContext:用两种不同颜色绘制两条线
我成立了一个小样本项目来处理这个问题。这是我的代码,我在drawRect方法中使用。我想画一个红色和蓝色线:
- (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);
}
感谢您的帮助
插入此代码设置笔触颜色的第二时间之前:
CGContextStrokePath(bluecontext);
CGContextBeginPath(bluecontext);
所有AddLine和AddOther调用正在构建一条路径。该路径使用最近设置的颜色和其他属性与像StrokePath一样的调用绘制。您正试图绘制两条单独的路径,因此您必须为每条路径调用“开始”和“中风”。当你开始绘画时,开始是隐含的,尽管你自己调用它并不会伤害它。绘图的基本流程是:
CGContextBeginPath(bluecontext); // clears any previous path
// add lines, curves, rectangles, etc...
CGContextStrokePath(bluecontext); // renders the path
我认为这可能是工作。
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
那就是你需要的。
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
如果你有兴趣,它看起来在一个循环的方式:
- (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);
}
}
答案中的transformationPoint是什么? – 2016-10-13 09:26:33
好点,好笑3年过去了,没有人想过要问这个问题。我很肯定你可以用“point”代替“transformedPoint”。我将在今天晚些时候进行测试以验证。 @Mady – 2016-10-13 15:37:48
昨天我做了这个,再次检查代码....但是,谢谢 – 2016-10-14 03:28:06
谢谢你,所以这基本上运行到这样的事实,我必须使用一种颜色的路径,我之前用另一种颜色画出一条路径? 创建两个上下文,一个用于蓝线,另一个用于红色的上下文不起作用,因为它们最后使用的是相同的上下文? – phonecoddy 2010-06-15 10:44:09
是的,对于每种颜色变化,您必须绘制路径并将其清除。你可以在同一个环境下做到这一切。 – drawnonward 2010-06-15 17:26:10