我使用UIBezierPath
的子类,以便使用touchEvents
和touchesEnded
绘制形状。
我正在制作一个应用程序(比如绘画),其中我有两种绘图模式(在drawRect中更改笔触颜色和宽度),当我在它们之间切换时,已经绘制的路径会改变颜色和widhts到新的。为了解决这个问题,在drawRect
时,我将当前路径保存在数组中并创建一个新路径。我想在制作新视图时重新绘制视图上保存的路径,以便它们可见。
在path.Stroke()
,我的数组中的foreach路径,我做public override void TouchesBegan (Foundation.NSSet touches, UIEvent evt)
{
base.TouchesBegan (touches, evt);
this.Path = new UIBezierPath ();
var touch = touches.AnyObject as UITouch;
var point = touch.LocationInView (this);
this.Path.MoveTo (point);
}
public override void TouchesMoved (Foundation.NSSet touches, UIEvent evt)
{
base.TouchesMoved (touches, evt);
var touch = touches.AnyObject as UITouch;
this.Path.AddLineTo(touch.LocationInView(this));
this.SetNeedsDisplay ();
}
public override void TouchesEnded (Foundation.NSSet touches, UIEvent evt)
{
base.TouchesEnded (touches, evt);
this.TouchesMoved (touches, evt);
paths.Add (Path);
}
public override void Draw (CGRect rect)
{
base.Draw (rect);
DrawSavedPaths ();
if (isScribbling)
{
this.Path.LineWidth = 2.5f;
this.PathColor.SetStroke ();
} else if (isMarking)
{
this.Path.LineWidth = 15.0f;
UIColor.FromRGBA (255, 0, 0, 0.3f).SetStroke ();
}
this.Path.Stroke ();
}
private void DrawSavedPaths()
{
if (paths.Count > 0)
{
foreach (UIBezierPath path in paths)
{
path.Stroke ();
}
}
}
,以便可视化它,但当我切换到某种绘图模式时,保存的路径再次改变它们的宽度和颜色
UIView
基本上,在我绘制的{{1}}中,我希望先前的路径是drewn,以防止它们被更改。