ios - 使用 MonoTouch 以编程方式绘制矩形
<p><p>我正在尝试更多地了解 MonoTouch,因此我尝试使用 Quartz2D 进行绘图。我想创建一个示例,在其中以编程方式绘制 n 矩形。问题是只绘制了第一个。我认为第二个被第一个删除/清除/覆盖。</p>
<p>这是我的代码:</p>
<p>SingleViewMTViewController.cs</p>
<pre><code>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;
views = tView;
views = tView2;
View.AddSubviews (views);
}
</code></pre>
<p>DraggableRectangle.cs</p>
<pre><code>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);
}
}
</code></pre>
<p>有没有更好的方法来用 MonoTouch 绘制独立的矩形?或者请有人解释一下,我做错了什么?</p>
<p><strong>更新:</strong> 顺便说一句。我可以达到的最佳输出,但这对于“黄色”和“棕色”是不正确的
<a href="http://www.bilder-upload.eu/show.php?file=81bfea-1329755225.png" rel="noreferrer noopener nofollow">http://www.bilder-upload.eu/show.php?file=81bfea-1329755225.png</a>
<a href="http://www.bilder-upload.eu/show.php?file=81bfea-1329755225.png" rel="noreferrer noopener nofollow">http://www.bilder-upload.eu/show.php?file=81bfea-1329755225.png</a> </p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>实际上,您的两个矩形都已绘制。问题是您的 <code>_targetLocation.Y</code> 值。您正在为 View 的 <code>Frame</code> 及其要在其 <code>Draw</code> 方法中绘制的矩形设置相同的 <strong>Y</strong> 值。</p>
<p>所以基本上,您的矩形是在 View 边界之外绘制的。您的“棕色” View 的高度是 40pt,而它的矩形绘制在 Y=100(在其可见部分下方)。</p>
<p>您必须区分这些值,因为 View 的位置总是相对于其父级的坐标。</p>
<p>编辑:伪代码示例。</p>
<p>下面一行:</p>
<pre><code>path.AddRect (new RectangleF (_targetLocation.X, _targetLocation.Y, _size.Width, _size.Height));
</code></pre>
<p>应该是这样的:</p>
<pre><code> path.AddRect (new RectangleF ({Frame.Width >= something >= 0},
{Frame.Height >= something >= 0}, width, height);
</code></pre>
<p>编辑#2:</p>
<p>如果我更改路径的矩形,请使用以下内容:</p>
<pre><code>path.AddRect (new RectangleF (0f, 0f, _size.Width, _size.Height));
</code></pre>
<p>这是我得到的......</p>
<p> <img src="/image/HqfuP.png" alt="Two rects"/> </p></p>
<p style="font-size: 20px;">关于ios - 使用 MonoTouch 以编程方式绘制矩形,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/9363891/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/9363891/
</a>
</p>
页:
[1]