代码之家  ›  专栏  ›  技术社区  ›  WhiteIntel

沙马林。UI上的iOS手指绘制

  •  0
  • WhiteIntel  · 技术社区  · 8 年前

    我想用一根手指在Xamarin iOS的UIImageView控件上绘制。 我发现: First Second

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

    1. 我必须用两根手指画画,但我只想用一根手指(不要与图片混淆,我必须用两根手指来激活触摸功能)
    2. 定位错误,我想问题在于缩放错误(见附图)

    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 回复  |  直到 8 年前
        1
  •  1
  •   Ax1le    8 年前

    定位错误,我想问题在于错误 缩放比例

    因为当我们使用图像时,图像的大小通常与 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
    

    关于

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

    你这是什么意思?该代码只能用手指绘制路径。