我正在尝试绘制一个具有描边的CGPath。
基本上,我想要使用CGPath画一条线。然后我想回去在最后一个CGPath的两侧画线,使其呈现轮廓效果。
这条线可以弯曲和转向,但我始终需要外部的两条线跟随它。
编辑:我需要能够使线的中间透明,但轮廓为纯黑色。
基本上,我想要使用CGPath画一条线。然后我想回去在最后一个CGPath的两侧画线,使其呈现轮廓效果。
这条线可以弯曲和转向,但我始终需要外部的两条线跟随它。
编辑:我需要能够使线的中间透明,但轮廓为纯黑色。
使用CGPathCreateCopyByStrokingPath
函数以指定的宽度描边旧路径并创建一个新路径。然后使用kCGPathFillStroke
绘制新路径。
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, 50, 50);
CGPathAddLineToPoint(path, NULL, 200, 200);
CGPathRef thickPath = CGPathCreateCopyByStrokingPath(path, NULL, 10, kCGLineCapButt, kCGLineJoinBevel, 0);
CGContextAddPath(context, thickPath);
CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);
CGContextSetFillColorWithColor(context, [UIColor blueColor].CGColor);
CGContextSetLineWidth(context, 3);
CGContextDrawPath(context, kCGPathFillStroke);
CGPathRelease(thickPath);
CGPathRelease(path);
}
CGPathCreateCopyByStrokingPath(...)
创建一个新路径,然后可以同时描边和填充。然后您可以使用半透明颜色。
CGPathCreateCopyByStrokingPath
Creates a stroked copy of another path.
CGPathRef CGPathCreateCopyByStrokingPath( CGPathRef path, const CGAffineTransform *transform, CGFloat lineWidth, CGLineCap lineCap, CGLineJoin lineJoin, CGFloat miterLimit );
Parameters
path
The path to copy.
transform
A pointer to an affine transformation matrix, or NULL if no transformation is needed. If specified, Quartz applies the transformation to elements of the converted path before adding them to the new path.
lineWidth
The line width to use, in user space units. The value must be greater than 0.
lineCap
A line cap style constant—kCGLineCapButt (the default), kCGLineCapRound, or kCGLineCapSquare. See “CGLineCap”.
lineJoin
A line join value—kCGLineJoinMiter (the default), kCGLineJoinRound, or kCGLineJoinBevel. See “CGLineJoin”.
miterLimit
The miter limit to use.
copy(strokingWithWidth:lineCap:lineJoin:miterLimit:transform:)
。 - David Rönnqvist