UI上的Xamarin.iOS手指画

时间:2018-03-23 01:09:28

标签: ios xamarin xamarin.ios uikit

我想用Xamarin iOS在UIImageView-Control上用一根手指画画。 我找到了什么: First Second

这两个示例无法正常工作,因为第一个链接存在以下问题:

  1. 我必须用两根手指画画,但我只想用一只手指(不要与图片混淆,我必须用两根手指来激活触控功能)
  2. 定位错误我猜这些问题依赖于错误的缩放(参见附图)
  3. enter image description here

    enter image description here

    enter image description here

    enter image description here

    enter image description here

    enter image description here

    enter image description here

    enter image description here

    enter image description here

    这是我目前的代码:

    public partial class DetailImageView : UIImageView
    {
        DetailImageViewController ctrl;
        public DetailImageView (IntPtr handle) : base (handle)
        {
    
        }
    
        public void Initialize(DetailImageViewController rootController)
        {
            ctrl = rootController;
        }
    
        public override void TouchesBegan(NSSet touches, UIEvent evt)
        {
            base.TouchesBegan(touches, evt);
    
            UITouch touch = touches.AnyObject as UITouch;
    
            drawOnTouch(touch);
    
            //DrawLineOnImage();
        }
    
        public override void TouchesMoved(NSSet touches, UIEvent evt)
        {
            base.TouchesMoved(touches, evt);
            UITouch touch = touches.AnyObject as UITouch;
    
            drawOnTouch(touch);
    
            //DrawLineOnImage();
        }
    
    
        private void drawOnTouch(object data)
        {
    
            UITouch touch = data as UITouch;
    
            if (null != touch)
            {
    
                UIGraphics.BeginImageContext(this.Image == null ? this.Frame.Size : this.Image.Size);
    
                //UIGraphics.BeginImageContext(this.Frame.Size);
    
                using (CoreGraphics.CGContext cont = UIGraphics.GetCurrentContext())
                {
    
                    if (this.Image != null)
                    {
    
                        cont.TranslateCTM(0f, this.Image.Size.Height);
                        cont.ScaleCTM(1.0f, -1.0f);
                        cont.DrawImage(new RectangleF(0f, 0f, Convert.ToSingle(this.Image.Size.Width)
                                                      , Convert.ToSingle(this.Image.Size.Height)), this.Image.CGImage);
                        cont.ScaleCTM(1.0f, -1.0f);
                        cont.TranslateCTM(0f, -this.Image.Size.Height);
    
    
                    } //end if
    
                    CGPoint lastLocation = touch.PreviousLocationInView(this);
                    CGPoint pt = touch.LocationInView(this);
                    using (CGPath path = new CGPath())
                    {
    
                        cont.SetLineCap(CGLineCap.Round);
                        cont.SetLineWidth(10);
                        cont.SetStrokeColor(0, 2, 3, 1);
    
                        var xMultiplier = Image.Size.Height / Frame.Height;
                        var yMultiplier = Image.Size.Width / Frame.Width;
    
                        var xLoc = lastLocation.X * xMultiplier;
                        var yLoc = lastLocation.Y * yMultiplier;
    
                        path.MoveToPoint(xLoc, yLoc);
                        path.AddLines(new CGPoint[] { new CGPoint(xLoc,
                                                                  yLoc),
                            new CGPoint(pt.X * xMultiplier, pt.Y * yMultiplier) });
                        path.CloseSubpath();
    
                        cont.AddPath(path);
                        cont.DrawPath(CGPathDrawingMode.FillStroke);
                        this.Image = UIGraphics.GetImageFromCurrentImageContext();
    
                    }//end using path
    
    
                }//end using cont
                UIGraphics.EndImageContext();
                this.SetNeedsDisplay();
    
            }//end if
        }//end void drawOnTouch
    }
    

1 个答案:

答案 0 :(得分:1)

  

定位错误我认为问题依赖于错误   缩放

因为当我们使用图片时,图片的大小通常与UIImageView的大小不同。您使用图像的大小来绘制路径,因此它会错过它的位置。尝试修改如下:

if (null != touch)
{

    UIGraphics.BeginImageContext(this.Frame.Size);

    using (CoreGraphics.CGContext cont = UIGraphics.GetCurrentContext())
    {

        if (this.Image != null)
        {

            cont.TranslateCTM(0f, this.Frame.Size.Height);
            cont.ScaleCTM(1.0f, -1.0f);
            cont.DrawImage(new RectangleF(0f, 0f, Convert.ToSingle(this.Frame.Size.Width)
                                            , Convert.ToSingle(this.Frame.Size.Height)), this.Image.CGImage);
            cont.ScaleCTM(1.0f, -1.0f);
            cont.TranslateCTM(0f, -this.Frame.Size.Height);


        } //end if

        CGPoint lastLocation = touch.PreviousLocationInView(this);
        CGPoint pt = touch.LocationInView(this);
        using (CGPath path = new CGPath())
        {

            cont.SetLineCap(CGLineCap.Round);
            cont.SetLineWidth(10);
            cont.SetStrokeColor(0, 2, 3, 1);

            var xLoc = lastLocation.X;
            var yLoc = lastLocation.Y;

            path.MoveToPoint(xLoc, yLoc);
            path.AddLines(new CGPoint[] { new CGPoint(xLoc,
                                                    yLoc),
            new CGPoint(pt.X, pt.Y) });
            path.CloseSubpath();

            cont.AddPath(path);
            cont.DrawPath(CGPathDrawingMode.FillStroke);
            this.Image = UIGraphics.GetImageFromCurrentImageContext();

        }//end using path


    }//end using cont
    UIGraphics.EndImageContext();
    this.SetNeedsDisplay();

}//end if

关于

  

我必须用两根手指画画,但我只想用一根   手指

你是什么意思?代码可以用于仅用手指绘制路径。