【问题标题】:Trying to draw a filled circle but it has got a black background试图画一个实心圆圈,但它有一个黑色的背景
【发布时间】: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


    【解决方案1】:

    看来我得放这个

    this.BackgroundColor = UIColor.Clear;
    

    进入构造函数。现在我不再有黑色背景了。

    使用这些代码行绘图似乎也足够了:

    context.AddEllipseInRect (rect);
    context.SetFillColor (color.CGColor);
    context.FillPath ();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-29
      • 2022-06-16
      • 2015-05-16
      • 2012-11-12
      • 1970-01-01
      • 1970-01-01
      • 2021-06-24
      • 2015-05-17
      相关资源
      最近更新 更多