[MonoTouch] Capturing a signature

Jason Awbrey jason at awbrey.net
Mon Mar 12 01:08:46 UTC 2012


here is the code for my DrawView.  This was heavily influenced by one of
the samples in Dimitris' MonoTouch
Cookbook<http://www.amazon.com/iOS-Development-using-MonoTouch-Cookbook/dp/1849691460>,
so he deserves most of the credit.

In my controller (not shown) I have buttons for Clear, Cancel and Save that
I display with the DrawView.  The DrawView has public methods for clearing
the canvas and returning a UIImage of the signature/drawing.


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


On Sat, Mar 10, 2012 at 4:30 PM, Dean Cleaver <
dean.cleaver at xceptionsoftware.com> wrote:

>  Awesome – thanx Jason. Will flick you an email tomorrow evening.****
>
> ** **
>
> Dino****
>
> ** **
>
> *From:* Jason Awbrey [mailto:jason at awbrey.net]
> *Sent:* Saturday, March 10, 2012 12:38 PM
> *To:* Dean Cleaver
> *Cc:* monotouch at lists.ximian.com
> *Subject:* Re: [MonoTouch] Capturing a signature****
>
> ** **
>
> I have code to do exactly this but am out of town without my Mac - remind
> me tomorrow night and I can send it to you
>
> Sent from my iPhone****
>
>
> On Mar 10, 2012, at 1:09 PM, Dean Cleaver <
> dean.cleaver at xceptionsoftware.com> wrote:****
>
>  I have to capture a customer’s signature on the iPhone screen, and save
> it for later use. Anyone got any pointers on how to go about it?****
>
>  ****
>
> I see that a UIResponder has touch events, so I can track a finger. Am
> presuming that if I track a finger, I can draw directly to the view, but
> how do I capture that drawing later? Or should I also be drawing to a
> UIImage in the background or something like that?****
>
>  ****
>
> Just wondered if anyone had any pointers to get me started in the right
> direction.****
>
>  ****
>
> Dino****
>
>  _______________________________________________
> MonoTouch mailing list
> MonoTouch at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/monotouch****
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ximian.com/pipermail/monotouch/attachments/20120311/255e74cf/attachment.html>


More information about the MonoTouch mailing list