如何创建圆形虚线作为CALayer?

hel*_*loB 2 objective-c calayer ios

我读了这篇文章,画了点缀(不是虚线!)线,2017年的IBDesignable关于绘制虚线(而不是虚线).但是,我对图形一般不太熟悉,我想知道如何用CALayer做到这一点(所以我不需要做整个获取当前图形上下文的事情).

我试图产生一个看起来像这样的虚线(白色部分,忽略背景):

虚线

这是我制作虚线的代码:

CAShapeLayer *shapelayer = [CAShapeLayer layer];
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:startPoint];
[path addLineToPoint:endPoint];
[path setLineCapStyle:kCGLineCapRound];
UIColor *fill = [UIColor whiteColor];
shapelayer.strokeStart = 0.0;
shapelayer.strokeColor = fill.CGColor;
shapelayer.lineWidth = 4.0;
shapelayer.lineJoin = kCALineJoinRound;
shapelayer.lineDashPattern = [NSArray arrayWithObjects:[NSNumber numberWithInt:4],[NSNumber numberWithInt:6 ], nil];
shapelayer.path = path.CGPath;

return shapelayer;
Run Code Online (Sandbox Code Playgroud)

如何镜像我引用的SO帖子中的代码但继续使用CALayer?

我尝试修改该帖子中的代码,如下所示:

UIBezierPath * path = [[UIBezierPath alloc] init];
[path moveToPoint:startPoint];
[path addLineToPoint:endPoint];
[path setLineWidth:8.0];
CGFloat dashes[] = { path.lineWidth, path.lineWidth * 2 };
[path setLineDash:dashes count:2 phase:0];
[path setLineCapStyle:kCGLineCapRound];
[path stroke];

CAShapeLayer *returnLayer = [CAShapeLayer layer];
returnLayer.path = path.CGPath;
return returnLayer;
Run Code Online (Sandbox Code Playgroud)

然而,这最终没有任何吸引力.

Har*_*n.P 6

我希望这能帮到您.

CAShapeLayer *circleLayer = [CAShapeLayer layer];
[circleLayer setPath:[[UIBezierPath bezierPathWithOvalInRect:CGRectMake(50, 50, 100, 100)] CGPath]];
circleLayer.lineWidth = 2.0;
[circleLayer setStrokeColor:[[UIColor redColor] CGColor]];
[circleLayer setFillColor:[[UIColor clearColor] CGColor]];
circleLayer.lineJoin = kCALineJoinRound;
circleLayer.lineDashPattern = [NSArray arrayWithObjects:[NSNumber numberWithInt:2],[NSNumber numberWithInt:3 ], nil];
// self.bgImage is parentView
[[self.bgImage layer] addSublayer:circleLayer];

self.bgImage.layer.borderWidth = 1.0f;
self.bgImage.layer.borderColor = [[UIColor blueColor]CGColor];
Run Code Online (Sandbox Code Playgroud)

我附加了图像的输出 在此输入图像描述