OGeek|极客世界-中国程序员成长平台

标题: ios - 使用 MonoTouch 以编程方式绘制矩形 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 11:43
标题: ios - 使用 MonoTouch 以编程方式绘制矩形

我正在尝试更多地了解 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



Best Answer-推荐答案


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

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

您必须区分这些值,因为 View 的位置总是相对于其父级的坐标。

编辑:伪代码示例。

下面一行:

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));

这是我得到的......

Two rects

关于ios - 使用 MonoTouch 以编程方式绘制矩形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9363891/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://jike.in/) Powered by Discuz! X3.4