[英]Finger drawing in Xamarin.iOS App (C#)
I make an app where the user can draw something with his fingers... I have no idea how I can do this...我制作了一个应用程序,用户可以用手指画一些东西......我不知道我怎么能做到这一点......
After research I found the code below.经过研究,我找到了下面的代码。
But with this code I can only draw one line.但是用这个代码我只能画一条线。 If I the touch ended and a new touch event begins the line continue on this point... The old line connect automaticly to the new one.
如果我触摸结束并且一个新的触摸事件开始,那么这条线会在这一点上继续......旧线会自动连接到新线。
So I want to create a new line on each touch.所以我想在每次触摸时创建一个新行。
Does someone know how this is working?有人知道这是如何工作的吗?
code代码
CGPath path;
CGPoint initialPoint;
CGPoint latestPoint;
public DrawView (IntPtr handle) : base (handle)
{
BackgroundColor = UIColor.White;
path = new CGPath();
}
public override void TouchesBegan(NSSet touches, UIEvent evt)
{
base.TouchesBegan(touches, evt);
UITouch touch = touches.AnyObject as UITouch;
if (touch != null)
{
initialPoint = touch.LocationInView(this);
}
}
public override void TouchesMoved(NSSet touches, UIEvent evt)
{
base.TouchesMoved(touches, evt);
UITouch touch = touches.AnyObject as UITouch;
if (touch != null)
{
latestPoint = touch.LocationInView(this);
SetNeedsDisplay();
}
}
public override void Draw(CGRect rect)
{
base.Draw(rect);
if (!initialPoint.IsEmpty)
{
//get graphics context
using (CGContext g = UIGraphics.GetCurrentContext())
{
//set up drawing attributes
g.SetLineWidth(2);
UIColor.Black.SetStroke();
//add lines to the touch points
if (path.IsEmpty)
{
path.AddLines(new CGPoint[] { initialPoint, latestPoint });
}
else
{
path.AddLineToPoint(latestPoint);
}
//add geometry to graphics context and draw it
g.AddPath(path);
g.DrawPath(CGPathDrawingMode.Stroke);
}
}
}
You can create a extra CGPath
to record the paths and draw them with CGContext
您可以创建一个额外的
CGPath
来记录路径并使用CGContext
绘制它们
Code :代码 :
public partial class DrawLine : UIView
{
CGPath pathtotal;
CGPath path;
CGPoint initialPoint;
CGPoint latestPoint;
public DrawLine(IntPtr handle) : base(handle)
{
BackgroundColor = UIColor.White;
pathtotal = new CGPath();
}
public override void TouchesBegan(NSSet touches, UIEvent evt)
{
base.TouchesBegan(touches, evt);
path = new CGPath();
UITouch touch = touches.AnyObject as UITouch;
if (touch != null)
{
initialPoint = touch.LocationInView(this);
}
}
public override void TouchesMoved(NSSet touches, UIEvent evt)
{
base.TouchesMoved(touches, evt);
UITouch touch = touches.AnyObject as UITouch;
if (touch != null)
{
latestPoint = touch.LocationInView(this);
SetNeedsDisplay();
}
}
public override void Draw(CGRect rect)
{
base.Draw(rect);
if (!initialPoint.IsEmpty)
{
//get graphics context
using (CGContext g = UIGraphics.GetCurrentContext())
{
//set up drawing attributes
g.SetLineWidth(2);
UIColor.Black.SetStroke();
//add lines to the touch points
if (path.IsEmpty)
{
path.AddLines(new CGPoint[] { initialPoint, latestPoint });
}
else
{
path.AddLineToPoint(latestPoint);
}
//add geometry to graphics context and draw it
pathtotal.AddPath(path);
g.AddPath(pathtotal);
g.DrawPath(CGPathDrawingMode.Stroke);
}
}
}
}
Test result:测试结果:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.