CGMutablePathRef改变颜色

时间:2011-12-22 10:00:57

标签: iphone core-graphics mkoverlay

我在其中一个示例中使用Apple提供的示例,用于在MKOverlayView上绘制CGPath。此时线条绘制为单色,但我想将其设置在路径上的不同点。

    - (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;
}

基本上在

CGPathAddLineToPoint(path, NULL, cgPoint.x, cgPoint.y);

线我想尽可能设置RGB颜色,以便沿途可以有不同的颜色。我可以使用

在具有上下文的CALayer上执行此操作
CGContextSetRGBStrokeColor(ctx, 0, 0, 0, 0.5);

但如果可能的话,我会迷失方向。

1 个答案:

答案 0 :(得分:3)

这是不可能的。笔画颜色是上下文的属性,而不是路径;在描边整个路径时,上下文使用其当前笔触颜色。没有办法告诉上下文“为此lineto使用此笔触颜色,并为此lineto使用此笔触颜色,”等等。

您需要自己保持每种颜色与每个线段之间的关联,并一次描边一段:移动到上一个点(或起点),绘制一条线到下一个点,设置你对那个片段和中风的颜色。

相关问题