2

私は、AppleがMKOverlayViewでCGPathを描画するためのサンプルの1つで提供している例を使用しています。現時点では、線は単色で描かれていますが、パスに沿ったさまざまなポイントにこれを設定したいと思います。

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

line途中で色を変えられるように、できればRGBカラーを設定したいと思います。を使用して、コンテキストを使用してCALayerでこれを実行できます。

CGContextSetRGBStrokeColor(ctx, 0, 0, 0, 0.5);

しかし、ここで可能であれば失われます。

4

1 に答える 1

3

それは不可能です。ストロークの色は、パスではなく、コンテキストの属性です。パス全体をストロークすると、コンテキストは現在のストロークの色を使用します。lineto「このストロークの色をこれに使用し、このストロークの色をこれに使用する」などのコンテキストを伝える方法はありませんlineto

各色と各線分の関連付けを自分で保持し、一度に1つのセグメントをストロークする必要があります。前の点(または始点)に移動し、次の点に線をプロットし、色を設定します。そのセグメント、およびストローク。

于 2011-12-22T16:59:54.290 に答える