That's great - thanx a lot Jason!

Dino

From: Jason Awbrey [mailto:[email protected]]
Sent: Sunday, March 11, 2012 8:09 PM
To: Dean Cleaver
Cc: [email protected]
Subject: Re: [MonoTouch] Capturing a signature

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 
<[email protected]<mailto:[email protected]>> 
wrote:
Awesome - thanx Jason. Will flick you an email tomorrow evening.

Dino

From: Jason Awbrey [mailto:[email protected]<mailto:[email protected]>]
Sent: Saturday, March 10, 2012 12:38 PM
To: Dean Cleaver
Cc: [email protected]<mailto:[email protected]>
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 
<[email protected]<mailto:[email protected]>> 
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
[email protected]<mailto:[email protected]>
http://lists.ximian.com/mailman/listinfo/monotouch

_______________________________________________
MonoTouch mailing list
[email protected]
http://lists.ximian.com/mailman/listinfo/monotouch

Reply via email to