Building an app with an electronic signature feature for ios possibly using mono touch?

前端 未结 1 1583
囚心锁ツ
囚心锁ツ 2021-01-06 21:12

This is just a quick question as I have googled and only found apps that already have this feature - but how do I go about creating an application that has the ability to ca

相关标签:
1条回答
  • 2021-01-06 22:07

    Xamarin's Component Store has a Signature Pad component that does this.

    I've also written something similar from scratch - it's not particularly difficult. The code would look something like this:

    public class DrawView : UIView
        {
    
            DrawViewController dvc;
    
            // clear the canvas
            public void Clear ()
            {
                drawPath.Dispose ();
                drawPath = new CGPath ();
                fingerDraw = false;
                SetNeedsDisplay ();
    
            }
    
            // pass in a reference to the controller, although I never use it
    and could probably remove it
            public DrawView (RectangleF frame, DrawViewController root) :
    base(frame)
            {
                dvc = root;
                this.drawPath = new CGPath ();
                this.BackgroundColor = UIColor.White;
    
            }
    
    
            private PointF touchLocation;
            private PointF prevTouchLocation;
            private CGPath drawPath;
            private bool fingerDraw;
    
            public override void TouchesBegan (MonoTouch.Foundation.NSSet
    touches, UIEvent evt)
            {
                base.TouchesBegan (touches, evt);
    
                UITouch touch = touches.AnyObject as UITouch;
                this.fingerDraw = true;
                this.touchLocation = touch.LocationInView (this);
                this.prevTouchLocation = touch.PreviousLocationInView (this);
                this.SetNeedsDisplay ();
    
            }
    
            public override void TouchesMoved (MonoTouch.Foundation.NSSet
    touches, UIEvent evt)
            {
                base.TouchesMoved (touches, evt);
    
                UITouch touch = touches.AnyObject as UITouch;
                this.touchLocation = touch.LocationInView (this);
                this.prevTouchLocation = touch.PreviousLocationInView (this);
                this.SetNeedsDisplay ();
            }
    
            public UIImage GetDrawingImage ()
            {
                UIImage returnImg = null;
    
                UIGraphics.BeginImageContext (this.Bounds.Size);
    
                using (CGContext context = UIGraphics.GetCurrentContext()) {
                    context.SetStrokeColor (UIColor.Black.CGColor);
                    context.SetLineWidth (5f);
                    context.SetLineJoin (CGLineJoin.Round);
                    context.SetLineCap (CGLineCap.Round);
                    context.AddPath (this.drawPath);
                    context.DrawPath (CGPathDrawingMode.Stroke);
                    returnImg = UIGraphics.GetImageFromCurrentImageContext ();
                }
    
                UIGraphics.EndImageContext ();
    
                return returnImg;
            }
    
    
            public override void Draw (RectangleF rect)
            {
                base.Draw (rect);
    
                if (this.fingerDraw) {
                    using (CGContext context = UIGraphics.GetCurrentContext()) {
                        context.SetStrokeColor (UIColor.Black.CGColor);
                        context.SetLineWidth (5f);
                        context.SetLineJoin (CGLineJoin.Round);
                        context.SetLineCap (CGLineCap.Round);
                        this.drawPath.MoveToPoint (this.prevTouchLocation);
                        this.drawPath.AddLineToPoint (this.touchLocation);
                        context.AddPath (this.drawPath);
                        context.DrawPath (CGPathDrawingMode.Stroke);
                    }
                }
            }
        }
    
    0 讨论(0)
提交回复
热议问题