2011-12-22 17 views
2

私は、MKOverlayViewでCGPathを描画するためにAppleが提供しているサンプルを使用しています。現時点では、線は単一の色として描画されますが、これをパスに沿って異なる点に設定したいと思います。私は可能な場合、それが道に沿って異なる着色することができるようにRGB色を設定したいCGMutablePathRef色を変更する

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

線に本質的に

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

。私はコンテキストを使用してCALayerでこれを行うことができます

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

ここで可能な場合は失われます。

答えて

3

これはできません。ストロークの色は、パスではなくコンテキストの属性です。コンテキスト全体がパス全体をストロークするときに、現在のストロークカラーが使用されます。このlinetoにこのストロークカラーを使用し、このストロークカラーをlinetoなどとすることはできません。

各色と各ラインセグメントとの関連付けを自分で行う必要があります。前のポイント(または始点)に移動し、次のポイントにラインをプロットし、そのセグメントにある色とストロークを設定します。