简体   繁体   English

绘图圆没有填充IOS

[英]Drawing circle without fill IOS

I am using this code to draw a circle inside a UIImageView. 我正在使用此代码在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];

I would like the circle to be empty, and to have only the border. 我希望圆圈是空的,只有边框。 How can I remove the fill? 我怎样才能删除填充物?

Set the fill color to transparent (alpha 0) 将填充颜色设置为透明(alpha 0)

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

AKA... AKA ...

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

( thanks Zev ) 谢谢Zev

The CAShapeLayer manual page explicitly says set fillColor to nil to perform no fill operation. CAShapeLayer手册页明确说明将fillColor设置为nil以不执行填充操作。 That is not the same as filling with a transparent color, which some have suggested doing, as the latter will overwrite existing contents making them clear/transparent, and should be considered a destructive operation. 这与填充透明颜色不同,有人建议这样做,因为后者将覆盖现有内容,使其清晰/透明,并应被视为破坏性操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM