gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:17:04 26 4
gpt4 key购买 nike

我想了解更多有关 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

最佳答案

实际上,您的两个矩形都已绘制。问题是您的 _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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com