iphone - CGMutablePathRef 改变颜色

标签 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);

line 如果可能的话,我想设置 RGB 颜色,以便它一路上可以有不同的颜色。我可以使用

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

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

最佳答案

这是不可能的。描边颜色是上下文的属性,而不是路径的属性;当您描边整个路径时,上下文将使用其当前描边颜色。无法告诉上下文“为该 lineto 使用此描边颜色,为该 lineto 使用此描边颜色”等。

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

关于iphone - CGMutablePathRef 改变颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8602222/

相关文章:

iphone - 如何从 UITextfield 获取值并将其发送到电子邮件?

iphone - 知道为什么这个图像屏蔽代码不起作用吗?

ios - 从 IOS 中的 UIImagview 检测最左边的黑色点

ios - 多个 UIBezierPath 的渐变

ios - MKCircle 平滑调整大小

iphone - iOS 应用程序启动时崩溃

iphone - 如何在导航栏下方添加显示今天日期的固定栏?

ios - MKOverlay 在建筑物下解散

iphone - Google Directions API 折线未添加到 map

iphone - 转换矩形 :toView: works different on iPad retina and iPad 2