Iphone CGMutablePathRef更改颜色
我正在使用苹果公司在他们的一个示例中提供的示例,在MKOverlayView上绘制CGPath。目前,这条线绘制为单一颜色,但我想在路径上的不同点设置这一点Iphone CGMutablePathRef更改颜色,iphone,core-graphics,mkoverlay,Iphone,Core Graphics,Mkoverlay,我正在使用苹果公司在他们的一个示例中提供的示例,在MKOverlayView上绘制CGPath。目前,这条线绘制为单一颜色,但我想在路径上的不同点设置这一点 - (CGPathRef)newPathForPoints:(MKMapPoint *)points pointCount:(NSUInteger)pointCount clipRect:(MKMapRect)mapRect
- (CGPathRef)newPathForPoints:(MKMapPoint *)points
pointCount:(NSUInteger)pointCount
clipRect:(MKMapRect)mapRect
zoomScale:(MKZoomScale)zoomScale
{
// The fastest way to draw a path in an MKOverlayView is to simplify the
// geometry for the screen by eliding points that are too close together
// and to omit any line segments that do not intersect the clipping rect.
// While it is possible to just add all the points and let CoreGraphics
// handle clipping and flatness, it is much faster to do it yourself:
//
if (pointCount < 2)
return NULL;
CGMutablePathRef path = NULL;
BOOL needsMove = YES;
#define POW2(a) ((a) * (a))
// Calculate the minimum distance between any two points by figuring out
// how many map points correspond to MIN_POINT_DELTA of screen points
// at the current zoomScale.
double minPointDelta = MIN_POINT_DELTA / zoomScale;
double c2 = POW2(minPointDelta);
MKMapPoint point, lastPoint = points[0];
NSUInteger i;
for (i = 1; i < pointCount - 1; i++)
{
point = points[i];
double a2b2 = POW2(point.x - lastPoint.x) + POW2(point.y - lastPoint.y);
if (a2b2 >= c2) {
if (lineIntersectsRect(point, lastPoint, mapRect))
{
if (!path)
path = CGPathCreateMutable();
if (needsMove)
{
CGPoint lastCGPoint = [self pointForMapPoint:lastPoint];
CGPathMoveToPoint(path, NULL, lastCGPoint.x, lastCGPoint.y);
}
CGPoint cgPoint = [self pointForMapPoint:point];
CGPathAddLineToPoint(path, NULL, cgPoint.x, cgPoint.y);
}
else
{
// discontinuity, lift the pen
needsMove = YES;
}
lastPoint = point;
}
}
#undef POW2
// If the last line segment intersects the mapRect at all, add it unconditionally
point = points[pointCount - 1];
if (lineIntersectsRect(lastPoint, point, mapRect))
{
if (!path)
path = CGPathCreateMutable();
if (needsMove)
{
CGPoint lastCGPoint = [self pointForMapPoint:lastPoint];
CGPathMoveToPoint(path, NULL, lastCGPoint.x, lastCGPoint.y);
}
CGPoint cgPoint = [self pointForMapPoint:point];
CGPathAddLineToPoint(path, NULL, cgPoint.x, cgPoint.y);
}
return path;
}
行我想设置一个RGB颜色,如果可能的话,这样它可以不同的颜色沿途。我可以通过使用
CGContextSetRGBStrokeColor(ctx, 0, 0, 0, 0.5);
但是如果可能的话,我会迷路。这是不可能的。笔划颜色是上下文的属性,而不是路径;在对整个路径进行笔划时,上下文使用其当前笔划颜色。无法告诉上下文“将此笔划颜色用于此
lineto
,将此笔划颜色用于此lineto
,”等等
您需要自己保持每种颜色和手头的每一条线段之间的关联,并一次绘制一条线段:移动到上一个点(或起点),将一条线绘制到下一个点,设置该线段的颜色,然后进行笔划
CGContextSetRGBStrokeColor(ctx, 0, 0, 0, 0.5);