在Xamarin.iOS应用程序中绘制手指(C#)

时间:2017-10-03 09:10:15

标签: c# ios uiview xamarin.ios drawing

我制作了一个应用,用户可以用手指画出一些东西...... 我不知道我怎么能这样做......

经过研究,我找到了下面的代码。

但是使用这段代码我只能画一行。如果触摸结束并且新的触摸事件开始,则该线继续此点...旧线路自动连接到新线路。

所以我想在每次触摸时创建一个新行。

有人知道这是如何运作的吗?

    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);
            }
        }
    }

1 个答案:

答案 0 :(得分:1)

您可以创建额外的CGPath来记录路径并使用CGContext

绘制它们

代码:

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);
            }
        }
    }
}

测试结果:

enter image description here