Custom map path line

若如初见. 提交于 2019-12-04 16:03:12

stroke first a path ( road with) with color 1, then change stroke width and color to a thinner line with road with * 0.6 in color 2, and stroke again.

Ok, I managed to figure it out based on Mathew's suggestion but with a few tweaks...

Essentially I created a stroked path using CGPathCreateCopyByStrokingPath and made the stroke slightly darker than the base color. Then created a transparentLayer with the path and stroke, used the blend mode kCGBlendModeDestinationAtop and drew another path with only a fill this time on top. I'm not sure if this is the best approach but appears to work well.

CGPathRef newpath =  CGPathCreateCopyByStrokingPath(path, NULL, lineWidth, kCGLineCapRound, kCGLineJoinMiter, 0.0);
CGContextAddPath(context, newpath);

CGContextSetStrokeColorWithColor(context, line.color.CGColor);
CGContextSetFillColorWithColor(context,  line.color.CGColor);
CGContextSetLineWidth(context, lineWidth * 0.3);
CGContextSetAlpha(context, 0.8f);
CGContextBeginTransparencyLayer (context, NULL);
CGContextStrokePath(context);
CGContextBeginPath(context);
CGContextAddPath(context, newpath);
CGContextFillPath(context);
CGContextEndTransparencyLayer(context);
CGContextSetBlendMode(context, kCGBlendModeDestinationAtop);
CGContextSetAlpha(context, 0.3f);
CGContextBeginPath(context);
CGContextAddPath(context, newpath);
CGContextFillPath(context);

CGPathRelease(path);
CGPathRelease(newpath);

EDIT Here is a simpler solution based on AlexWien's approach

    if (path != nil)
    {

        CGPathRef  path2 = CGPathCreateCopy(path);
        CGFloat hue;
        CGFloat saturation;
        CGFloat brightness;
        CGFloat alpha;

        [line.color getHue:&hue saturation:&saturation brightness:&brightness alpha:&alpha];

        UIColor *c2 =[UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:0.4];
        CGContextAddPath(context, path);

        CGContextSetStrokeColorWithColor(context, c2.CGColor);
        CGContextSetLineJoin(context, kCGLineJoinRound);
        CGContextSetLineCap(context, kCGLineCapRound);
        CGContextSetLineWidth(context, lineWidth);
        CGContextStrokePath(context);
        CGPathRelease(path);

             CGContextSetBlendMode(context, kCGBlendModeSourceAtop);
            CGContextAddPath(context, path2);
              CGContextSetRGBStrokeColor(context, 1.0f, 1.0f, 1.0f, 0.3f);
            CGContextSetLineJoin(context, kCGLineJoinRound);
            CGContextSetLineCap(context, kCGLineCapRound);
            CGContextSetLineWidth(context, lineWidth/2.0f);
            CGContextStrokePath(context);
            CGPathRelease(path2);

    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!