绘图圆没有填充IOS

时间:2013-02-26 20:14:35

标签: ios xcode

我正在使用此代码在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 个答案:

答案 0 :(得分:18)

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

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

AKA ......

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

谢谢Zev

答案 1 :(得分:4)

CAShapeLayer手册页明确说明将fillColor设置为nil以执行无填充操作。这与填充透明颜色不同,有人建议这样做,因为后者将覆盖现有内容,使其清晰/透明,并且应被视为破坏性操作。