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];

我希望圆圈是空的,并且只有边框。我怎样才能去除填充物?

4

2 回答 2

17

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

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

又名...

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

感谢泽夫

于 2013-02-26T20:33:58.307 回答
6

CAShapeLayer 手册页明确指出将 fillColor 设置为nil不执行填充操作。这与填充透明颜色不同,有些人建议这样做,因为后者会覆盖现有内容,使它们变得清晰/透明,应该被视为破坏性操作。

于 2016-12-07T05:48:37.617 回答