gpt4 book ai didi

iphone - CGMutablePathRef 改变颜色

转载 作者:行者123 更新时间:2023-12-03 20:53:06 33 4
gpt4 key购买 nike

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

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com