2

我正在使用 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);

但是如果这里可能的话,我会迷路。

4

1 回答 1

3

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

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

于 2011-12-22T16:59:54.290 回答