使用 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
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
实际上,你的两个矩形都已绘制。问题在于您的
_targetLocation.Y
值。您为视图的Frame
及其要在其Draw
方法中绘制的矩形设置相同的 Y 值。所以基本上,您的矩形是在视图边界之外绘制的。你的“棕色”视图的高度是 40pt,而它的矩形是在 Y=100(在其可见部分下方)绘制的。
您必须区分这些值,因为视图的位置始终相对于其父级的坐标。
编辑:伪代码示例。
以下行:
应该类似于:
编辑 #2:
如果我更改路径的矩形,则使用以下内容:
这是我得到的...
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'sFrame
and its rectangle to be drawn in itsDraw
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:
Should be something like:
Edit #2:
If I change the rect of the path, with the following:
Here is what I get...