Default Green Orange
Marcus Follrud
Wish I had a slogan
RSS
  • Home Page Home
  • Cruel Chipmunk Productions
  • Om
  • Projects

#MonoTouch how to – Drag and Drop Image

monotouch, Programmering Add comments

My blog posts are commonly in Swedish, but since this might interest users outside the borders of Sweden I decided to write this post in English.

Today, I’ll show how easy it is to Drag an Image around the screen using MonoTouch. Bear with me though, it’s my first how to :) .

It’s actually really simple, and you won’t need the Interface Builder.

What we need to do is to create an UIImageView class that overrides the functions TouchesBegan, TouchesMoved and TouchesEnded. It’s looking like this:

	public class myDraggableImage : UIImageView {

		//Store locations for remembering the last positions, and counting the future ones.
		PointF Location;
		PointF StartLocation;

		bool haveBeenTouchedOnce = false;

		public myDraggableImage ( RectangleF frame ){

					//Set the position of the frame with RectangleF (Replacement of CGRectangle)
					this.Frame = frame;
					StartLocation = this.Frame.Location;
		}

		//This event occurs when you just touch the object
		public override void TouchesBegan (MonoTouch.Foundation.NSSet touches, MonoTouch.UIKit.UIEvent e)
		{
			Console.WriteLine("Touched the object");
			Location = this.Frame.Location;

			var touch = (UITouch) e.TouchesForView (this).AnyObject;
			var bounds = Bounds;

			StartLocation = touch.LocationInView(this);
			this.Frame = new RectangleF(Location,bounds.Size);

		}
		//This event occurs when you drag it around
		public override void TouchesMoved (MonoTouch.Foundation.NSSet touches, MonoTouch.UIKit.UIEvent e)
		{
			Console.WriteLine("Dragged the object");
			var bounds = Bounds;
			var touch = (UITouch) e.TouchesForView (this).AnyObject;

			//Always refer to the StartLocation of the object that you've been dragging.
			Location.X += touch.LocationInView(this).X - StartLocation.X;
			Location.Y += touch.LocationInView(this).Y - StartLocation.Y;

			this.Frame = new RectangleF(Location,bounds.Size);

			haveBeenTouchedOnce = true;
		}

		public override void TouchesEnded (MonoTouch.Foundation.NSSet touches, MonoTouch.UIKit.UIEvent e)
		{
			StartLocation = Location;
		}

	}

What’s being done here is that as soon as we Touch the object it will run ”TouchesBegan” to start keeping track of the Object. And when we are moving it around, it constantly calls ”TouchesMoved” where we are updating the position of the UIImageView on the screen.

When you’ve implemented the class class in your project, all you need to do is to create the object within ”public override bool FinishedLaunching”.
It can for instance look like this:

myDraggableImage img = new myDraggableImage(new RectangleF(64,64,64,64));
			img.UserInteractionEnabled = true;
			img.BackgroundColor = UIColor.Green;
			img.Hidden = false;

			window.AddSubview(img);

The result will be a UIImageView that can be dragged around the screen.

la la laaa
  • Facebook
  • Twitter
Be Sociable, Share!
  • Tweet

september 23rd, 2009  
Tags: c#, monotouch, UIImageView

4 Responses to “#MonoTouch how to – Drag and Drop Image”

  1. MonoTouch.Info
    september 23rd, 2009 at 10:29

    MonoTouch Article – How to Drag and Drop an Image…

    Thank you for submitting this entry – Trackback from MonoTouch.Info…


  2. expist-online
    juli 1st, 2010 at 14:41

    thanks

    SvaraSvara

  3. vishnu
    januari 11th, 2012 at 11:00

    Mange Tak Marcus!

    Perfect article for me, helps aptly with my current project. :)

    Was wondering if you had any other such interesting articles on programming with Monotouch [the existing material on the web isn't much]. Please, Please, Be’ om post them if you do.

    Also, can you please give me a picture on the amount of usage of Monotouch amongst your development community. Plus, is the app submission process as problematic as it was when it comes to submission of apps developed using Monotouch?

    Never mind if you wouldn’t want to spend time on sending me a response. Thanks again for the article.

    Cheers!
    Vishnu

    SvaraSvara

  4. marcus
    januari 17th, 2012 at 13:40

    No probs Vishnu,

    I’m glad it helped you out =)

    I haven’t done much MonoTouch development recently, but if I do you can expect some more content here =)

    I’d check monotouch.info. It’s a good aggregation service for everything MonoTouch. Also i think the Xamarian mailing list for monotouch is a good way to communicate with Monotouch devs and users :-)

    Cheers!

    SvaraSvara

Leave a Reply

  • Kategorier

    • Linux
    • monotouch
    • Open source
    • Planet
    • Privat
    • Programmering
    • Spel
  • Arkiv

    • januari 2012
    • september 2011
    • augusti 2011
    • januari 2010
    • december 2009
    • november 2009
    • oktober 2009
    • september 2009
    • augusti 2009
    • juli 2009
  • Etiketter

    2.6.30 16f690 Andjelka ANSI C apparmor apple axis biltema blommor c# curl debian despotify django drupal inaktivitet iphone kde Linux linux mint liseberg mono monotouch novell Open source php pic pic16f690 plasma plasmoid Programmering pump pykde pyqt python qt sdcc sommarstuga spotify spytify vatten vattna blommor virtualbox wordpress youtube
Copyright © 2012 Marcus Follrud
XHTML CSS Logga in