<?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>Chris-Software.com &#187; Transformation</title>
	<atom:link href="http://chris-software.com/index.php/tag/transformation/feed/" rel="self" type="application/rss+xml" />
	<link>http://chris-software.com</link>
	<description>iPhone, iPod touch games, Objective-C Tutorials, krzysztofrutkowski.com</description>
	<lastBuildDate>Tue, 29 Mar 2011 09:58:11 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Animations and transformations</title>
		<link>http://chris-software.com/index.php/2009/04/26/animations-and-transformations/</link>
		<comments>http://chris-software.com/index.php/2009/04/26/animations-and-transformations/#comments</comments>
		<pubDate>Sun, 26 Apr 2009 18:01:50 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[Obj-C Tutorials]]></category>
		<category><![CDATA[Animation]]></category>
		<category><![CDATA[Transformation]]></category>
		<category><![CDATA[UIView]]></category>

		<guid isPermaLink="false">http://chris-software.com/?page_id=618</guid>
		<description><![CDATA[Using right animation / transformation can improve quality of your application. There are most important during game development.
You can use 3 basic transformations &#8211; you can marge them or invent your own. These 3 animations are:

rotation
scale
translation


You can apply animation to almost everything &#8211; images (UIImageView), buttons (UIButton), toolbars, label, views&#8230;
Rotation:
Let&#8217;s say you have an image [...]]]></description>
			<content:encoded><![CDATA[<p>Using right animation / transformation can improve quality of your application. There are most important during game development.</p>
<p>You can use 3 basic transformations &#8211; you can marge them or invent your own. These 3 animations are:</p>
<ul>
<li>rotation</li>
<li>scale</li>
<li>translation</li>
</ul>
<p style="text-align: center; "><a href="http://chris-software.com/wp-content/uploads/2009/04/animationsimulator.png"><img class="size-medium wp-image-619 aligncenter" title="animationsimulator" src="http://chris-software.com/wp-content/uploads/2009/04/animationsimulator-161x300.png" alt="animationsimulator" width="161" height="300" /></a></p>
<p>You can apply animation to almost everything &#8211; images (<em>UIImageView</em>), buttons (<em>UIButton</em>), toolbars, label, views&#8230;</p>
<h2>Rotation:</h2>
<p>Let&#8217;s say you have an image (<em>UIImageView *myImage</em>) and you would like to do something with it. To rotate it:</p>
<pre>myImage.transform = CGAffineTransformMakeRotation(M_PI*0.25);</pre>
<p>This will rotate image by 45 degrees right. Here are the typical values and their degree equivalents:</p>
<ul>
<li><em>M_PI * 0.00</em> = 0° (original condition)</li>
<li><em>M_PI * 0.25</em> = 45°</li>
<li><em>M_PI * 0.50<span style="font-style: normal;"> = 90</span>°<br />
</em></li>
<li><em>M_PI * 0.75 = </em>135<em>°<br />
</em></li>
<li><em>M_PI * 1.00 = </em>180<em>°<br />
</em></li>
<li><em>M_PI * -0.75 = </em>225<em>°<br />
</em></li>
<li><em>M_PI * -0.50 = </em>270<em>°<br />
</em></li>
<li><em>M_PI * -0.25 = </em>315<em>°<br />
</em></li>
</ul>
<p>If you would like to use degrees not the range &lt;-1 ; 1&gt; multiplied by <em>M_PI</em> you can define the constant value in header of your class: <em>#define radianConst M_PI/</em><span><em>180.0</em> (right under the <em>#import</em> lines). Now if you want to turn your <em>image</em> by 63 degrees use this:</span></p>
<pre>myImage.transform = CGAffineTransformMakeRotation(63.0*radianConst);</pre>
<h2><span>Scale</span></h2>
<p>Scale allows you to zoom in / zoom out your <em>image</em>. It&#8217;s more than simple scale, you can set the scale parameter different for <em>image&#8217;s</em> width and height. If you would like to have the <em>image</em> 2 times wider and 1.5 times higher try this:</p>
<pre>myImage.transform = CGAffineTransformMakeScale(2.0,1.5);</pre>
<p>Don&#8217;t be surprised that after this operation your image will be pixeled, that&#8217;s why in future you should provide images in higher resolution make them smaller on load (for example <em>CGAffineTransformMakeScale(0.5,0.5)</em>) and later when needed show them in original dimensions using <em>CGAffineTransformMakeScale(1.0,1.0)</em>.</p>
<p>Scale gives you one more cool effect. If you set scale parameter as a negative value your image will be scaled and flipped horizontally or vertically. Examples:</p>
<ul>
<li><em>CGAffineTransformMakeScale(-1.0,2.0)</em> &#8211; <em>image</em> flipped horizontally and twice higher</li>
<li><em>CGAffineTransformMakeScale(1.5,-3.0) &#8211; image</em> 1.5 times wider, 3 times higher and flipped vertically</li>
<li><em>CGAffineTransformMakeScale(-2.0,-2.0)</em> &#8211; <em>image </em>2 times bigger and flipped both horizontally and vertically</li>
</ul>
<h2>Translation</h2>
<p>Translation is doing almost the same thing as changing the <em>center</em> value of any object. It moves <em>image</em> along X and Y axises. It&#8217;s the poorest so I will give you only 1 example and won&#8217;t talk about this any more.</p>
<ul>
<li><em>CGAffineTransformMakeTranslation(50.0,20.0)</em> &#8211; <em>image</em> moved 50 pixels to the right and 20 to the botton</li>
</ul>
<p>Similar effect you will get using <em>center</em> as I mentioned before: <em>myImage.center = CGPointMake(140.0,90.0)</em>. Both of them has their own advantages and disadvantages, but let&#8217;s now move to something more interesting.</p>
<h2>Animations</h2>
<p>You know how to rotate an <em>image</em> (or anything else) by given degree, scale, flip, or move. How about creating a smooth animation? Well it&#8217;s very simple:</p>
<pre><span><span>	</span>[</span><span>UIView</span><span> </span>beginAnimations<span>:</span><span>nil</span><span> </span>context<span>:</span><span>NULL</span><span>];
<span><span>	</span>[</span><span>UIView</span><span> </span>setAnimationDuration<span>:</span><span>1.5</span><span>];
	// other animations goes here
<span>	myImage</span><span>.transform = </span>CGAffineTransformMakeRotation(M_PI*0.5<span>);
	// other animations goes here
<span><span>	</span>[</span><span>UIView</span><span> </span>commitAnimations<span>];</span></span></span></span></pre>
<p>This code will smoothly rotate your object by 45 degress during 1.5 seconds. Please note, that while performing any animation the application continue to run next tasks &#8211; doesn&#8217;t hold for a given amount of seconds. If you would like your app to do an action when the animation is finished you can use either the <em>NSTimer</em>:</p>
<pre><span>[</span><span>NSTimer</span><span> </span><span>scheduledTimerWithTimeInterval</span><span>:1.5</span><span> </span><span>target</span><span>:</span><span>self</span><span> </span><span>selector</span><span>:</span><span>@selector</span><span>(nextMethodName</span><span>) </span><span>userInfo</span><span>:</span><span>nil</span><span> </span><span>repeats</span><span>:NO</span><span>];</span></pre>
<p>or extend your animation code by it&#8217;s <em>delegate</em> and <em>selector</em>:</p>
<pre><span><span>	</span>[</span><span>UIView</span><span> </span>beginAnimations<span>:</span><span>nil</span><span> </span>context<span>:</span><span>NULL</span><span>];
<span><span>	</span>[</span><span>UIView</span><span> </span>setAnimationDuration<span>:</span><span>1.5</span><span>];
	[UIView setAnimationDelegate:self];
	[UIView setAnimationDidStopSelector:@selector(nextMethodName)];
<span>	myImage</span><span>.transform = </span>CGAffineTransformMakeRotation(M_PI*0.5<span>);
	// other animations goes here
<span><span>	</span>[</span><span>UIView</span><span> </span>commitAnimations<span>];</span></span></span></span></pre>
<p>Remember that second example is much more efficient, but in that case, <em>nextMethodName</em> (whatever you name it) must return a <em>boolean</em> <em>YES</em> &#8211; not <em>void</em> which doesn&#8217;t return any value at all. Example:</p>
<pre>-(BOOL)nextMethodName {
 	// some tasks</pre>
<pre>	return YES;
}</pre>
<p>Animations can be great to have transitions between two views but I will talk about them in other article.</p>
<h2><strong>Two transformations at once</strong></h2>
<p>Using any of above transformations you are modyfing only one property &#8211; <em>transform</em> of a given object. For example the code:</p>
<pre>
<pre>myImage.transform = CGAffineTransformMakeScale(2.0,1.5);
myImage.transform = CGAffineTransformMakeRotation(M_PI*1.0);</pre>
</pre>
<p>will give you the image rotated by 180 degrees, but won&#8217;t be scaled &#8211; scale was canceled after making rotation.</p>
<p>If you would like to merge two transform use the following code:</p>
<pre><span>animateLabel</span><span>.transform = </span>CGAffineTransformConcat<span>(</span>CGAffineTransformMakeScale<span>(2.0</span><span>, 1.5</span><span>), </span>CGAffineTransformMakeRotation<span>(</span><span>M_PI</span><span>*1.0</span><span>));</span></pre>
<p>I personally really don&#8217;t like this. I didn&#8217;t enjoy matrices during Mathematics in school. Any transform creates the matrix and <em>CGAffineTransformConcat</em> is multiplying two matrices &#8211; the order is necessary. If you wanted to merge three transforms you would have to use <em>CGAffineTransformConcat</em> merging two transforms as an argument of another <em>CGAffineTransformConcat</em> mergin previous merge with next transform. Sound difficult? Yes, for me two.</p>
<h2>Original shape</h2>
<p>To conclude, if you would like to quickly cancel any transforms use <em>CGAffineTransformIdentity</em> as a <em>transform</em> property of any object.</p>
<pre>myImage.transform = CGAffineTransformIdentity;</pre>
<p>I guess it&#8217;s all you need to know about transforms and animations right now. Below you can download my project where you can rotate, scale, translate any object using sliders and watch the smooth scale animation.</p>
<p style="text-align: center;"><a href="http://chris-software.com/wp-content/uploads/2009/04/animations.zip"><img class="alignnone size-full wp-image-564" title="xcodeproj" src="http://chris-software.com/wp-content/uploads/2009/04/xcodeproj.png" alt="xcodeproj" width="128" height="128" /></a></p>
<p style="text-align: center;"><a href="http://chris-software.com/wp-content/uploads/2009/04/animations.zip">Download the project</a></p>
]]></content:encoded>
			<wfw:commentRss>http://chris-software.com/index.php/2009/04/26/animations-and-transformations/feed/</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
	</channel>
</rss>

