c# - Xamarin.iOS 应用程序 (C#) 中的手指绘图

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

最佳答案

你可以创建一个额外的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

关于c# - Xamarin.iOS 应用程序 (C#) 中的手指绘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46541038/

相关文章:

c# - 找到基于两个输入的唯一输出?

ios - 难以捉摸的 UIView 子类名称

objective-c - 显示 View 时 iOS 背景变暗

ios - 截取 UIView iOS 的屏幕截图

c# - 使用 OpenXML 从 MVC Controller 将工作簿设为只读

c# - 有人能解决这个问题吗?我无法连接到本地主机 "Xamarin" "C"

c# - 在没有 .ToList() 复制操作的情况下将 List<Concrete> 转换为 List<Inherited Interface>

ios - 快速将 JSON 转换为 ManagedObject 以获得可选参数

objective-c - 保留 iOS setter 方法中的发布惯例

ios - UIActivityViewController 在共享到 Gmail 应用程序时无法设置主题