使用 MonoTouch 以编程方式绘制矩形

发布于 2025-01-06 23:26:38 字数 2660 浏览 0 评论 0原文

我正在尝试了解有关 MonoTouch 的更多信息,因此我尝试使用 Quartz2D 进行绘图。我想创建一个示例,在其中以编程方式绘制 n 个矩形。问题是只绘制了第一个。我认为第二个被第一个删除/清除/覆盖。

这是我的代码:

SingleViewMTViewController.cs

public override void ViewDidLoad ()
    {
        base.ViewDidLoad ();

        PointF locationOne = new PointF (5, 5);
        PointF locationTwo = new PointF (5, 100);
        SizeF size = new SizeF (120, 40);

        DraggableRectangle tView = new DraggableRectangle (locationOne, size, UIColor.Yellow);
        DraggableRectangle tView2 = new DraggableRectangle (locationTwo, size, UIColor.Brown);

        DraggableRectangle[] views = new DraggableRectangle[2];
        views [0] = tView;
        views [1] = tView2;

        View.AddSubviews (views);
    }

DraggableRectangle.cs

public class DraggableRectangle : UIView
{
    private CGPath path;
    private PointF _targetLocation;
    private SizeF _size;
    private UIColor _fillColor = UIColor.Brown;

    public DraggableRectangle (PointF targetLocation, SizeF size)
    {
        _targetLocation = targetLocation;
        _size = size;

        RectangleF frameRect = new RectangleF (_targetLocation.X, _targetLocation.Y, _size.Width, _size.Height);
        this.Frame = frameRect;
        this.BackgroundColor = UIColor.Clear; //without this, nothing is drawn
    }

    public DraggableRectangle (PointF targetLocation, SizeF size, UIColor fillColor):this(targetLocation,size)
    {
        _fillColor = fillColor;
    }

    public override void Draw (RectangleF rect)
    {
        //base.Draw (rect);
        //works without base-call?

        //get graphics context
        using (CGContext gctx = UIGraphics.GetCurrentContext ()) {

            //set up drawing attributes
            _fillColor.SetFill ();

            //create geometry
            path = new CGPath ();

            path.AddRect (new RectangleF (_targetLocation.X, _targetLocation.Y, _size.Width, _size.Height));

            path.CloseSubpath ();

            //add geometry to graphics context and draw it
            gctx.AddPath (path);        
            gctx.DrawPath (CGPathDrawingMode.FillStroke);   
        }       
    }

有没有更好的方法用 MonoTouch 绘制独立的矩形?或者请有人解释一下,我做错了什么?

更新:顺便说一句。我能实现的最佳输出,但这对于“黄色”和“棕色”来说是不正确的 http://www.bilder-upload.eu/show。 php?file=81bfea-1329755225.png http://www.bilder-upload.eu/show。 php?file=81bfea-1329755225.png

I am trying to learn more about MonoTouch and so I tried out drawing with Quartz2D. I want to create an example, where I draw n-rectangles programmatically. The problem is that only the first one is drawn. I think that the second one is deleted/cleared/covered by the first one.

Here is my code:

SingleViewMTViewController.cs

public override void ViewDidLoad ()
    {
        base.ViewDidLoad ();

        PointF locationOne = new PointF (5, 5);
        PointF locationTwo = new PointF (5, 100);
        SizeF size = new SizeF (120, 40);

        DraggableRectangle tView = new DraggableRectangle (locationOne, size, UIColor.Yellow);
        DraggableRectangle tView2 = new DraggableRectangle (locationTwo, size, UIColor.Brown);

        DraggableRectangle[] views = new DraggableRectangle[2];
        views [0] = tView;
        views [1] = tView2;

        View.AddSubviews (views);
    }

DraggableRectangle.cs

public class DraggableRectangle : UIView
{
    private CGPath path;
    private PointF _targetLocation;
    private SizeF _size;
    private UIColor _fillColor = UIColor.Brown;

    public DraggableRectangle (PointF targetLocation, SizeF size)
    {
        _targetLocation = targetLocation;
        _size = size;

        RectangleF frameRect = new RectangleF (_targetLocation.X, _targetLocation.Y, _size.Width, _size.Height);
        this.Frame = frameRect;
        this.BackgroundColor = UIColor.Clear; //without this, nothing is drawn
    }

    public DraggableRectangle (PointF targetLocation, SizeF size, UIColor fillColor):this(targetLocation,size)
    {
        _fillColor = fillColor;
    }

    public override void Draw (RectangleF rect)
    {
        //base.Draw (rect);
        //works without base-call?

        //get graphics context
        using (CGContext gctx = UIGraphics.GetCurrentContext ()) {

            //set up drawing attributes
            _fillColor.SetFill ();

            //create geometry
            path = new CGPath ();

            path.AddRect (new RectangleF (_targetLocation.X, _targetLocation.Y, _size.Width, _size.Height));

            path.CloseSubpath ();

            //add geometry to graphics context and draw it
            gctx.AddPath (path);        
            gctx.DrawPath (CGPathDrawingMode.FillStroke);   
        }       
    }

Is there a better way to draw independent rectangles with MonoTouch? Or could please someone explain, what I am doing wrong?

UPDATE: Thats btw. the best output I can achieve, but thats just not correct for "Yellow" and "Brown"
http://www.bilder-upload.eu/show.php?file=81bfea-1329755225.png
http://www.bilder-upload.eu/show.php?file=81bfea-1329755225.png

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心安伴我暖 2025-01-13 23:26:38

实际上,你的两个矩形都已绘制。问题在于您的 _targetLocation.Y 值。您为视图的 Frame 及其要在其 Draw 方法中绘制的矩形设置相同的 Y 值。

所以基本上,您的矩形是在视图边界之外绘制的。你的“棕色”视图的高度是 40pt,而它的矩形是在 Y=100(在其可见部分下方)绘制的。

您必须区分这些值,因为视图的位置始终相对于其父级的坐标。

编辑:伪代码示例。

以下行:

path.AddRect (new RectangleF (_targetLocation.X, _targetLocation.Y, _size.Width, _size.Height));

应该类似于:

    path.AddRect (new RectangleF ({Frame.Width >= something >= 0}, 
{Frame.Height >= something >= 0}, width, height);

编辑 #2:

如果我更改路径的矩形,则使用以下内容:

path.AddRect (new RectangleF (0f, 0f, _size.Width, _size.Height));

这是我得到的...

两个矩形

Actually, both of your rectangles are drawn. The problem is your _targetLocation.Y value. You are setting the same Y value for both your view's Frame and its rectangle to be drawn in its Draw method.

So basically, your rectangle is drawn outside the bounds of the view. Your "brown" view's height is 40pt, while its rectangle is drawn at Y=100 (below its visible portion).

You have to differentiate these values, since a view's location is always relative to its parent's coordinates.

Edit: a pseudo-code example.

The following line:

path.AddRect (new RectangleF (_targetLocation.X, _targetLocation.Y, _size.Width, _size.Height));

Should be something like:

    path.AddRect (new RectangleF ({Frame.Width >= something >= 0}, 
{Frame.Height >= something >= 0}, width, height);

Edit #2:

If I change the rect of the path, with the following:

path.AddRect (new RectangleF (0f, 0f, _size.Width, _size.Height));

Here is what I get...

Two rects

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文