在iOS中绘制无填充圆形

12

我正在使用这段代码在UIImageView内绘制一个圆形。

CAShapeLayer *circleLayer = [CAShapeLayer layer];
// Give the layer the same bounds as your image view
[circleLayer setBounds:CGRectMake(0.0f, 0.0f, [photoView bounds].size.width,
                                  [photoView bounds].size.height)];
// Position the circle anywhere you like, but this will center it
// In the parent layer, which will be your image view's root layer
[circleLayer setPosition:CGPointMake(coordsFinal.x + 130, coordsFinal.y + 200)];
// Create a circle path.
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:
                      CGRectMake(0.0f, 0.0f, 50.0f, 50.0f)];
// Set the path on the layer
[circleLayer setPath:[path CGPath]];
// Set the stroke color
[circleLayer setStrokeColor:[color CGColor]];
// Set the stroke line width
[circleLayer setLineWidth:2.0f];

// Add the sublayer to the image view's layer tree
[[photoView layer] addSublayer:circleLayer];

我希望将圆形变为空心,只保留边框。如何去除填充颜色?

2个回答

17

将填充颜色设置为透明(alpha 0)

[circleLayer setFillColor:[[UIColor colorWithWhite:0 alpha:0] CGColor]];

AKA...

[circleLayer setFillColor:[[UIColor clearColor] CGColor]];

(感谢 Zev)


6
CAShapeLayer手册明确表示将fillColor设置为nil来执行不填充操作。这与用透明色进行填充不同,有些人建议这样做,后者会覆盖现有内容,使它们变得清晰/透明,并且应该被视为一种破坏性的操作。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接