<?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; delay</title>
	<atom:link href="http://chris-software.com/index.php/tag/delay/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>Timers</title>
		<link>http://chris-software.com/index.php/2009/04/27/timers/</link>
		<comments>http://chris-software.com/index.php/2009/04/27/timers/#comments</comments>
		<pubDate>Mon, 27 Apr 2009 17:14:27 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[Obj-C Tutorials]]></category>
		<category><![CDATA[action]]></category>
		<category><![CDATA[animate]]></category>
		<category><![CDATA[delay]]></category>
		<category><![CDATA[load]]></category>
		<category><![CDATA[NSTimer]]></category>
		<category><![CDATA[repeat]]></category>
		<category><![CDATA[timers]]></category>

		<guid isPermaLink="false">http://chris-software.com/?page_id=630</guid>
		<description><![CDATA[Timers &#8211; believe me or not, but they are useful not only in programming stopwatches.
Let&#8217;s give some examples.

To show a welcome screen and to close it after several seconds
You are making a game and you would like to display an info how long user is playing current stage, without comparing NSDate (start and current).
To create [...]]]></description>
			<content:encoded><![CDATA[<p><em>Timers</em> &#8211; believe me or not, but they are useful not only in programming stopwatches.</p>
<p>Let&#8217;s give some examples.</p>
<ul>
<li>To show a welcome screen and to <strong>close</strong> it after several seconds</li>
<li>You are making a game and you would like to display an info how long user is playing current stage, without comparing <em>NSDate </em>(start and current).</li>
<li>To create an animation without fade in / fade out effect.</li>
</ul>
<p>Using timer (<em>NSTimer</em>) you are able to launch any task after given time or repeat it periodically.</p>
<div>I will give you an description with code for each of above examples.</div>
<h2>Welcome screen</h2>
<p>Welcome screen is shown only once during launch so there is no need to create any information in header file of your class about <em>NSTimer</em>. All you need to do is to add this line to your <em>viewDidLoad</em> method:</p>
<pre><span><span>	</span>[</span><span>NSTimer</span><span> </span>scheduledTimerWithTimeInterval<span>:</span><span><strong>1.5</strong></span><span> </span>target<span>:</span><span>self</span><span> </span>selector<span>:</span><span>@selector</span><span>(closeWelcomeScreen) </span>userInfo<span>:</span><span>nil</span><span> </span>repeats<span>:</span><span><strong>NO</strong></span><span>];</span></pre>
<div>And to develop a <em>closeWelcomeScreen</em> method:</div>
<pre>-(void)closeWelcomeScreen {
	[welcomeScreen removeFromSuperview];
}</pre>
<div><em>closeWelcomeScreen</em> will be called in 1.5 seconds after <em>view</em> was loaded. Remember to set the <em>repeats</em> property to <em>NO</em> this time. If you set <em>YES</em>, the method <em>closeWelcomeScreen</em> would be called every 1.5 seconds.</div>
<div>Right now we are simply removing the <em>welcomeScreen</em>, of course it&#8217;s up to you if you decide to perform an animation to change it&#8217;s <em>alpha</em> property it or to add something on top of <em>welcomeScreen</em>.</div>
<h2>How long?</h2>
<p>Now we will create a <em>timer</em> that will call the method every <em>0.01</em> second. In header file we need to add a <em>float</em> variable (<em>float time;</em>) and it&#8217;s also a <em>label</em> to show somewhere the value of this variable:</p>
<pre><span>@interface</span> MyView : UIViewController {
<span><span>	</span></span><span>IBOutlet</span><span> </span><span>UILabel</span><span> *</span>myLabel;
<span>	</span>float time;
}</pre>
<p>Now in <em>viewDidLoad</em> or any other method you will call by yourself, add:</p>
<pre><span><span>	</span>[</span><span>NSTimer</span><span> </span>scheduledTimerWithTimeInterval<span>:0.01</span><span> </span>target<span>:</span><span>self</span><span> </span>selector<span>:</span><span>@selector</span><span>(countTime) </span>userInfo<span>:</span><span>nil</span><span> </span>repeats<span>:<strong>YES</strong></span><span>];</span></pre>
<p>And you need to implement <em>countTime</em> method:</p>
<pre>-(<span>void</span>)countTime {
<span>	</span>time += <span>0.01</span>;
<span><span>	</span></span><span>myLabel</span><span>.</span><span>text</span><span> = [</span><span>NSString</span><span> </span>stringWithFormat<span>:</span><span>@"%.2f"</span><span>,time];
}</span></pre>
<p>As you see, this time <em>NSTimer&#8217;s</em> <em>repeats</em> property is set to <em>YES</em>, and that&#8217;s why it will call <em>countTime</em> method every 0.01 second.</p>
<h2>Animations using <em>NSTimer</em></h2>
<p>As I told you before using typical animations (and transformations) (<span><em>[</em></span><span><em>UIView</em></span><span><em> </em></span><span><em>beginAnimations</em></span><span><em>:</em></span><span><em>nil</em></span><span><em> </em></span><span><em>context</em></span><span><em>:</em></span><span><em>NULL</em></span><span><em>]</em> and <span><em>[</em></span><span><em>UIView</em></span><span><em> </em></span><span><em>commitAnimations</em></span><span><em>]</em>) you get the nice fade in and out effect. To perform a rotation for example you have to create a similar <em>timer</em> like in code above, with tiny difference. It won&#8217;t be changing <em>label&#8217;s</em> text but transforming it using each time bigger angle.</span></span></p>
<pre>
<pre><span><span>	</span>[</span><span>NSTimer</span><span> </span>scheduledTimerWithTimeInterval<span>:0.01</span><span> </span>target<span>:</span><span>self</span><span> </span>selector<span>:</span><span>@selector</span><span>(countTime) </span>userInfo<span>:</span><span>nil</span><span> </span>repeats<span>:YES</span><span>];
-(<span>void</span>)countTime {
<span>	</span>rotation += <span>0.001</span>;
<span><span>	</span></span><span>myLabel</span><span>.transform</span><span> = CGAffineTransformMakeRotation(M_PI*rotation)</span><span>;
}</span> </span></pre>
</pre>
<p>Remember to add in your header file <em>rotation</em> variable: <em>float rotation;</em></p>
<h2>Stoping <em>NSTimer</em></h2>
<p>In each of above examples when <em>NSTimer</em> repeats itself you can&#8217;t stop it &#8211; the <em>time </em>will grow and/or your object will be rotated. To obtain it you have to add the <em>NSTimer</em> object to the header and later in implementation to use it&#8217;s <em>invalidate</em> method whenever/wherever you want to stop it. Below I show you an example of the <em>timer</em> that will make a full rotation of <em>myLabel</em> and invaliate itself.</p>
<p>Header:</p>
<pre><span>@interface</span> MyView : UIViewController {
<span><span>	</span></span><span>IBOutlet</span><span> </span><span>UILabel</span><span> *</span>myLabel<span>;
<span><span>	</span></span><span>float</span><span> </span>rotation<span>;
<span>	</span><span>NSTimer</span> *rotationTimer;
}</span></span></pre>
<p>Starting the timer:</p>
<pre><span><span>	</span></span><span>rotationTimer</span><span> = [</span><span>NSTimer</span><span> </span>scheduledTimerWithTimeInterval<span>:</span><span>0.01</span><span> </span>target<span>:</span><span>self</span><span> </span>selector<span>:</span><span>@selector</span><span>(rotate) </span>userInfo<span>:</span><span>nil</span><span> </span>repeats<span>:</span><span>YES</span><span>];</span></pre>
<div>And the <em>rotate</em> method:</div>
<pre>-(<span>void</span>)rotate {
<span>	</span><span>if</span> (<span>rotation</span> &gt;= <span>2.0</span>) {
<span><span>		</span>[</span>rotationTimer<span> </span><span>invalidate</span><span>];
<span><span>		</span></span>return<span>;
<span>	</span>}
<span><span>	</span></span>rotation<span> += </span><span>0.001</span><span>;
<span><span>	</span></span><span>myLabel</span><span>.transform = </span>CGAffineTransformMakeRotation<span>(</span><span>M_PI</span><span>*</span><span>rotation</span><span>);
}</span></span></span></span></pre>
<div>You can call <em>invalidate</em> method anywhere in your project. A <em>button</em> can <em>invalidate</em> your timer or the timer itself as shown above.</div>
<h2>Personal experience</h2>
<p>Timers can be more complex than just counting a time or perform a simple task. If you use timer as a run loop of your game for example, be careful. If many <em>methods</em> can invalidate the timer you should check if it cannot be invalidated twice sometimes. It will crash your application. Although <em>NSTimer</em> has the boolean method <em>isValid</em>, once <em>NSTimer</em> is invalidated you can&#8217;t use this property. Declaring any <em>NSTimer</em> I also add the <em>boolean</em> variable with similar name:</p>
<pre>NSTimer *myTimer;
bool myTimerInvalidated;</pre>
<p>Each time I start the <em>timer</em> I set the <em>boolean</em> equals <em>NO</em>, each time I want to invalidate the <em>timer</em> I use:</p>
<pre>if (!myTimerInvalidated) {
	myTimerInvalidated = YES;
	[myTimer invalidate];
}</pre>
<p>Download the sample project to see live example of <em>NSTimers</em>.</p>
<p style="text-align: center;"><a href="http://chris-software.com/wp-content/uploads/2009/04/timers.zip"><img class="size-full wp-image-564 aligncenter" 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/timers.zip">Download the project</a></p>
]]></content:encoded>
			<wfw:commentRss>http://chris-software.com/index.php/2009/04/27/timers/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
	</channel>
</rss>

