test drawing code

by SS on July 23rd, 2010
No notes
Syntax: C#
Show lines - Hide lines - Show in textbox - Download
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Drawing;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using MonoTouch.CoreGraphics;
using MonoTouch.CoreAnimation;
 
namespace TestDraw
{
	public class PaintingImageView : UIImageView
	{
		PointF Location;
	    PointF StartLocation;
		UIView mainView;
 
		float width;
		float height;
 
		CGColor blueColor = new CGColor(0f, 0f, 1f, 1f);
		public CGBitmapContext bitmapContext;
	    bool haveBeenTouchedOnce = false;
 
	    public PaintingImageView (RectangleF frame, UIView view)
		{
			this.Frame = frame;
			StartLocation = this.Frame.Location;
			this.mainView = view;
 
			width = this.Frame.Width;
			height = this.Frame.Height;
 
			bitmapContext = new CGBitmapContext(IntPtr.Zero, (int)width, (int)height, 8, (int)height * 4,
		                                                    CGColorSpace.CreateDeviceRGB(), CGImageAlphaInfo.PremultipliedFirst);
		}
 
	    public override void TouchesBegan (MonoTouch.Foundation.NSSet touches, MonoTouch.UIKit.UIEvent e)
	    {
			haveBeenTouchedOnce = true;
	        Console.WriteLine("Touched the object");
 
	        Location = this.Frame.Location;
 
	        var touch = (UITouch)e.TouchesForView(this).AnyObject;
	        var bounds = Bounds;
 
	        StartLocation = touch.LocationInView(this);
 
			Console.WriteLine ("X = " + StartLocation.X);
			Console.WriteLine ("Y = " + StartLocation.Y);
 
			bitmapContext.SetFillColorWithColor(blueColor);
			bitmapContext.FillEllipseInRect(new RectangleF(StartLocation.X, this.Frame.Height - StartLocation.Y, 20f, 20f));
 
			this.Image = UIImage.FromImage(bitmapContext.ToImage());
 
			mainView.AddSubview(this);
 
	        this.Frame = new RectangleF(Location,bounds.Size);
	    }
 
	    public override void TouchesMoved (MonoTouch.Foundation.NSSet touches, MonoTouch.UIKit.UIEvent e)
	    {
	        var bounds = Bounds;
	        var touch = (UITouch) e.TouchesForView (this).AnyObject;
 
	        Location.X += touch.LocationInView(this).X - StartLocation.X;
	        Location.Y += touch.LocationInView(this).Y - StartLocation.Y;
 
			UIGraphics.BeginImageContext(this.Frame.Size);
 
 
			bitmapContext.FillEllipseInRect(new RectangleF(touch.LocationInView(this).X, this.Frame.Height - touch.LocationInView(this).Y, 20f, 20f));
 
			this.Image = UIImage.FromImage(bitmapContext.ToImage());
 
			mainView.AddSubview(this);
	        haveBeenTouchedOnce = false;
	    }
 
	    public override void TouchesEnded (MonoTouch.Foundation.NSSet touches, MonoTouch.UIKit.UIEvent e)
	    {
			Console.WriteLine ("Ended Touch the object");
			StartLocation = Location;
	    }
	}
}

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS