ios - 在 MKPolyLine View 中绘制渐变

标签 ios objective-c calayer cagradientlayer

我的 MKPolyLineView 有问题。我只是尝试对多段线进行颜色渐变,但使用 CAGradient 它确实有效。我将 MKPolylineView 子类化并在

中重新绘制
- (void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context

 UIColor *darker = [UIColor blackColor];
    CGFloat baseWidth = self.lineWidth / zoomScale;

    // draw the dark colour thicker
    CGContextAddPath(context, self.path);
    CGContextSetStrokeColorWithColor(context, darker.CGColor);
    CGContextSetLineWidth(context, baseWidth * 1.5);
    CGContextSetLineCap(context, self.lineCap);
    CGContextStrokePath(context);

    // now draw the stroke color with the regular width
    CGContextAddPath(context, self.path);
    CGContextSetStrokeColorWithColor(context, self.strokeColor.CGColor);
    CGContextSetLineWidth(context, baseWidth);
    CGContextSetLineCap(context, self.lineCap);
    CGContextStrokePath(context);

    [super drawMapRect:mapRect zoomScale:zoomScale inContext:context];
}

但即使这样也行不通(StrokeColor = red)。任何想法如何进入渐变 折线? (高色、中心色、低色)

谢谢大家

最佳答案

要用渐变绘制 MKPolyline,您可以使用 MKPolylineView 的自定义子类。由于 CoreGraphics 不支持描边渐变路径,我们必须

  1. 使用 CGPathCreateCopyByStrokingPath 将路径转换为跟踪路径边缘的形状
  2. 将上下文剪裁成该形状
  3. 使用 CGContextDrawLinearGradient 填充

这是一个让你入门的子类:

@interface TWOGradientPolylineView : MKPolylineView

@end

@implementation TWOGradientPolylineView

- (void)strokePath:(CGPathRef)path inContext:(CGContextRef)context
{
    CGFloat lineWidth = CGContextConvertSizeToUserSpace(context, (CGSize){self.lineWidth, self.lineWidth}).width;
    CGPathRef pathToFill = CGPathCreateCopyByStrokingPath(path, NULL, lineWidth, self.lineCap, self.lineJoin, self.miterLimit);
    CGRect rect = CGPathGetBoundingBox(pathToFill);
    CGContextAddPath(context, pathToFill);
    CGPathRelease(pathToFill);
    CGContextClip(context);

    CGFloat gradientLocations[2] = {0.0f, 1.0f};
    CGFloat gradientColors[8] = {1.0f, 0.0f, 0.0f, 0.75f, 1.0f, 1.0f, 0.0f, 0.75f};
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGGradientRef gradient = CGGradientCreateWithColorComponents(colorSpace, gradientColors, gradientLocations, 2);
    CGColorSpaceRelease(colorSpace);

    CGPoint gradientStart = rect.origin;
    CGPoint gradientEnd = {CGRectGetMaxX(rect), CGRectGetMaxY(rect)};

    CGContextDrawLinearGradient(context, gradient, gradientStart, gradientEnd, kCGGradientDrawsAfterEndLocation);
    CGGradientRelease(gradient);
}

@end

这是用上面的类绘制的路径的截图:

screenshot of a path with a gradient

关于ios - 在 MKPolyLine View 中绘制渐变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15382883/

相关文章:

ios - 每次将应用程序推回前台时刷新 tableView 的最有效方法是什么?

objective-c - 使用 opengl View 时出现 "renderincontext"问题

ios - 在 UITableViewCell 的 subview 上设置 mask 层会覆盖自动布局约束

ios - 如何在图层内部绘制并仅在 [view Layer addSublayer :newLayer]?

iphone - 异步进程给我带来了麻烦

ios - 细节 View 顶部的额外空间不会消失(SwiftUI)

objective-c - typedef 类 : "cannot find interface declaration for ' typedefname'"上的 ObjC 类别

ios - 如何使用自定义单元格大小提高 UICollectionView 的性能?

cocoa - CALayer 的 ShadowPath 中存在错误?

ios - 如何更改 UIBezierPath 笔画颜色而不重绘它