xcode 没有填充IOS的画圆
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15098391/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Drawing circle without fill IOS
提问by user2014474
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?
我希望圆圈是空的,并且只有边框。我怎样才能去除填充物?
回答by foundry
Set the fill color to transparent (alpha 0)
将填充颜色设置为透明(alpha 0)
[circleLayer setFillColor:[[UIColor colorWithWhite:0 alpha:0] CGColor]];
AKA...
又名...
[circleLayer setFillColor:[[UIColor clearColor] CGColor]];
(thanks Zev)
(感谢泽夫)
回答by clearlight
The CAShapeLayer manual page explicitly says set fillColor to nil
to perform no fill operation. 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.
CAShapeLayer 手册页明确说明将 fillColor 设置为nil
不执行填充操作。这与填充透明颜色不同,有些人建议这样做,因为后者会覆盖现有内容,使它们变得清晰/透明,应被视为破坏性操作。