Iphone CoreGraphics多色线
我有以下代码,似乎只使用最后一种颜色的整行。。。。。 我希望颜色在整个过程中不断变化。有什么想法吗Iphone CoreGraphics多色线,iphone,core-graphics,Iphone,Core Graphics,我有以下代码,似乎只使用最后一种颜色的整行。。。。。 我希望颜色在整个过程中不断变化。有什么想法吗 CGContextSetLineWidth(ctx, 1.0); for(int idx = 0; idx < routeGrabInstance.points.count; idx++) { CLLocation* location = [routeGrabInstance.points objectAtIndex:
CGContextSetLineWidth(ctx, 1.0);
for(int idx = 0; idx < routeGrabInstance.points.count; idx++)
{
CLLocation* location = [routeGrabInstance.points objectAtIndex:idx];
CGPoint point = [mapView convertCoordinate:location.coordinate toPointToView:self.mapView];
if(idx == 0)
{
// move to the first point
UIColor *tempColor = [self colorForHex:[[routeGrabInstance.pointHeights objectAtIndex:idx] doubleValue]];
CGContextSetStrokeColorWithColor(ctx,tempColor.CGColor);
CGContextMoveToPoint(ctx, point.x, point.y);
}
else
{
UIColor *tempColor = [self colorForHex:[[routeGrabInstance.pointHeights objectAtIndex:idx] doubleValue]];
CGContextSetStrokeColorWithColor(ctx,tempColor.CGColor);
CGContextAddLineToPoint(ctx, point.x, point.y);
}
}
CGContextStrokePath(ctx);
CGContextSetLineWidth(ctx,1.0);
对于(int idx=0;idx
CGContextSetStrokeColorWithColor
在上下文中设置笔划颜色。该颜色在绘制路径时使用,在继续构建路径时没有任何效果
您需要分别笔划每一行(
CGContextStrokePath
)。CGContextSetStrokeColorWithColor仅更改上下文的状态,不进行任何绘图。代码中唯一完成的绘图是最后的CGContextStrokePath。由于每次调用CGContextSetStrokeColorWithColor都会覆盖上一次调用设置的值,因此图形将使用最后一个颜色集
您需要创建一个新路径,设置颜色,然后在每个循环中绘制。大概是这样的:
for(int idx = 0; idx < routeGrabInstance.points.count; idx++)
{
CGContextBeginPath(ctx);
CGContextMoveToPoint(ctx, x1, y1);
CGContextAddLineToPoint(ctx, x2, y2);
CGContextSetStrokeColorWithColor(ctx,tempColor.CGColor);
CGContextStrokePath(ctx);
}
for(int idx=0;idx
谢谢,我通过-(void)drawLayer:(CALayer*)layer inContext:(CGContextRef)ctx调用此功能{这会给我带来任何问题吗?我如何创建另一个上下文?+1,但你应该重写“第一次使用”部分,这很混乱,因为笔划实际上是最后一次发生的。你不必创建另一个上下文,只需创建一条路径并在for循环的每个循环中(对于每一行)加上笔划即可。