<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Marcus Follrud &#187; UIImageView</title>
	<atom:link href="http://marcusfollrud.net/tag/uiimageview/feed/" rel="self" type="application/rss+xml" />
	<link>http://marcusfollrud.net</link>
	<description>Wish I had a slogan</description>
	<lastBuildDate>Thu, 22 Mar 2012 12:11:14 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<atom:link rel="hub" href="http://pubsubhubbub.appspot.com"/><atom:link rel="hub" href="http://superfeedr.com/hubbub"/>		<item>
		<title>#MonoTouch how to &#8211; Drag and Drop Image</title>
		<link>http://marcusfollrud.net/2009/09/23/monotouch-how-to-drag-and-drop-image/</link>
		<comments>http://marcusfollrud.net/2009/09/23/monotouch-how-to-drag-and-drop-image/#comments</comments>
		<pubDate>Wed, 23 Sep 2009 05:32:00 +0000</pubDate>
		<dc:creator>marcus</dc:creator>
				<category><![CDATA[monotouch]]></category>
		<category><![CDATA[Programmering]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[UIImageView]]></category>

		<guid isPermaLink="false">http://marcusfollrud.net/?p=312</guid>
		<description><![CDATA[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&#8217;ll show how easy it is to Drag an Image around the screen using MonoTouch. Bear with me though, it&#8217;s my first how to . It&#8217;s actually really [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fmarcusfollrud.net%2F2009%2F09%2F23%2Fmonotouch-how-to-drag-and-drop-image%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fmarcusfollrud.net%2F2009%2F09%2F23%2Fmonotouch-how-to-drag-and-drop-image%2F&amp;source=marcusfollrud&amp;style=normal&amp;service=bit.ly&amp;service_api=R_7d280395e19104feae6bc0cd839f41c0&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>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.</p>
<p>Today, I&#8217;ll show how easy it is to <em>Drag an Image </em>around the screen using MonoTouch. Bear with me though, it&#8217;s my first how to <img src='http://marcusfollrud.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
<p>It&#8217;s actually really simple, and you won&#8217;t need the Interface Builder.</p>
<p>What we need to do is to create an UIImageView class that overrides the functions <strong>TouchesBegan</strong>, <strong>TouchesMoved</strong> and <strong>TouchesEnded</strong>. It&#8217;s looking like this:</p>
<pre name="code" class="c-sharp">	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;
		}

	}</pre>
<p>What&#8217;s being done here is that as soon as we Touch the object it will run &#8221;TouchesBegan&#8221; to start keeping track of the Object. And when we are moving it around, it constantly calls &#8221;TouchesMoved&#8221; where we are updating the position of the UIImageView on the screen.</p>
<p>When you&#8217;ve implemented the class class in your project, all you need to do is to create the object within &#8221;<span style="color: #0000ff;">public override bool</span> FinishedLaunching&#8221;.<br />
It can for instance look like this:</p>
<pre  name="code" class="c-sharp">myDraggableImage img = new myDraggableImage(new RectangleF(64,64,64,64));
			img.UserInteractionEnabled = true;
			img.BackgroundColor = UIColor.Green;
			img.Hidden = false;

			window.AddSubview(img);</pre>
<p>The result will be a UIImageView that can be dragged around the screen.</p>
<p><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="425" height="344" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="allowFullScreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="src" value="http://www.youtube.com/v/Hq-CvzW_D0E&amp;hl=sv&amp;fs=1" /><param name="allowfullscreen" value="true" /><embed type="application/x-shockwave-flash" width="425" height="344" src="http://www.youtube.com/v/Hq-CvzW_D0E&amp;hl=sv&amp;fs=1" allowscriptaccess="always" allowfullscreen="true"></embed></object></p>
]]></content:encoded>
			<wfw:commentRss>http://marcusfollrud.net/2009/09/23/monotouch-how-to-drag-and-drop-image/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

