【发布时间】:2014-12-05 11:21:12
【问题描述】:
我想用某种颜色画一个圆,填满整个圆。我正在使用自动布局来确定圆圈的大小。如果我这样做,则绘制圆圈但背景颜色为黑色。它应该是一种清晰的颜色,以便背景可以发光,当然圆圈应该是圆形的。这是我用 C# 编写的代码,但它与 Objective-C 没有太大区别。
public class Circle : UIView
{
private UIColor color;
public Circle ()
{
this.color = UIColor.Red;
}
public override void Draw (RectangleF rect)
{
base.Draw (rect);
this.BackgroundColor = UIColor.Clear;
float red = 0;
float green = 0;
float blue = 0;
float alpha = 0;
color.GetRGBA (out red, out green, out blue, out alpha);
float lineWidth = 2.0f;
RectangleF borderRect = RectangleF.Inflate (rect, -lineWidth/2, -lineWidth/2);
// Get the context
CGContext context = UIGraphics.GetCurrentContext ();
// Set the border width
context.SetLineWidth (lineWidth);
// Set the circle fill color
context.SetRGBFillColor (red, green, blue, alpha);
// Set the circle border color
context.SetRGBStrokeColor (red, green, blue, alpha);
// Fill the circle with the fill color
context.FillEllipseInRect (borderRect);
// Draw the circle border
//context.StrokeEllipseInRect (borderRect);
}
}
}
圆比UIView 略小,因此可以正确绘制,不接触边缘(因此切割某些部分)。
但是你可以在这里看到背景是黑色的:
如何画一个实心圆?
【问题讨论】:
标签: ios uiview core-graphics drawrect cgcontext