<?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>iPhoneGeek 爱疯极客 &#187; 定时器</title>
	<atom:link href="http://www.iphone-geek.cn/tag/%e5%ae%9a%e6%97%b6%e5%99%a8/feed" rel="self" type="application/rss+xml" />
	<link>http://www.iphone-geek.cn</link>
	<description>iPhone 新闻，编程，技巧与提示，代码，教程</description>
	<lastBuildDate>Thu, 08 Dec 2011 01:18:52 +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>跨平台代码分享之二 &#8211; 定时器</title>
		<link>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/%e8%b7%a8%e5%b9%b3%e5%8f%b0%e4%bb%a3%e7%a0%81%e5%88%86%e4%ba%ab%e4%b9%8b%e4%ba%8c-%e5%ae%9a%e6%97%b6%e5%99%a8</link>
		<comments>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/%e8%b7%a8%e5%b9%b3%e5%8f%b0%e4%bb%a3%e7%a0%81%e5%88%86%e4%ba%ab%e4%b9%8b%e4%ba%8c-%e5%ae%9a%e6%97%b6%e5%99%a8#comments</comments>
		<pubDate>Fri, 20 Nov 2009 03:58:03 +0000</pubDate>
		<dc:creator>bagusflyer</dc:creator>
				<category><![CDATA[编程]]></category>
		<category><![CDATA[跨平台]]></category>
		<category><![CDATA[定时器]]></category>
		<category><![CDATA[源代码]]></category>

		<guid isPermaLink="false">http://www.iphone-geek.cn/?p=278</guid>
		<description><![CDATA[定时器的应用实在是太广了，从普通应用程序到游戏，大部分程序都会用到定时器。如果你打算让你的应用程序跨越平台的限制，一个跨平台的定时器是必不可少的。上一次，我给大家分享了一段有关线程的代码。实际上，定时器的是以线程为基础的，在这里我仍然是使用pthread，当然大家可以直接继承我的utils::CThread类。
&#160;

timer.h：
&#160;
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667/*
&#160;* &#160;timer.h
&#160;* &#160;utils
&#160;*
&#160;* &#160;Created by Bagusflyer on 09-6-2.
&#160;* &#160;Copyright 2009 www.iphone-geek.cn. All rights reserved.
&#160;*
&#160;*/

#ifndef _IPHONEGEEK_UTILS_TIMER_H_
#define _IPHONEGEEK_UTILS_TIMER_H_

#include &#60;pthread.h&#62;

namespace utils
&#123;
&#160; &#160; 
&#160; &#160; class CTimer;
&#160; &#160; 
&#160; &#160; class CTimerCallbackInterface
&#160; &#160; &#123;
&#160; &#160; public:
&#160; &#160; &#160; &#160; virtual void onTimer&#40;const CTimer* timer&#41; = 0;
&#160; &#160; &#125;;

&#160; &#160; class CTimer 
&#160; &#160; &#123;
&#160; &#160; public: &#160; &#160; 
&#160; [...]]]></description>
			<content:encoded><![CDATA[<p>定时器的应用实在是太广了，从普通应用程序到游戏，大部分程序都会用到定时器。如果你打算让你的应用程序跨越平台的限制，一个跨平台的定时器是必不可少的。上一次，我给大家分享了一段有关线程的代码。实际上，定时器的是以线程为基础的，在这里我仍然是使用pthread，当然大家可以直接继承我的utils::CThread类。</p>
<p>&nbsp;</p>
<p><span id="more-278"></span></p>
<h2>timer.h：</h2>
<p>&nbsp;</p>
<div class="codecolorer-container cpp mac-classic" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br />61<br />62<br />63<br />64<br />65<br />66<br />67<br /></div></td><td><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #ff0000; font-style: italic;">/*<br />
&nbsp;* &nbsp;timer.h<br />
&nbsp;* &nbsp;utils<br />
&nbsp;*<br />
&nbsp;* &nbsp;Created by Bagusflyer on 09-6-2.<br />
&nbsp;* &nbsp;Copyright 2009 www.iphone-geek.cn. All rights reserved.<br />
&nbsp;*<br />
&nbsp;*/</span><br />
<br />
<span style="color: #339900;">#ifndef _IPHONEGEEK_UTILS_TIMER_H_</span><br />
<span style="color: #339900;">#define _IPHONEGEEK_UTILS_TIMER_H_</span><br />
<br />
<span style="color: #339900;">#include &lt;pthread.h&gt;</span><br />
<br />
<span style="color: #0000ff;">namespace</span> utils<br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #0000ff;">class</span> CTimer<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #0000ff;">class</span> CTimerCallbackInterface<br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">public</span><span style="color: #008080;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">virtual</span> <span style="color: #0000ff;">void</span> onTimer<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> CTimer<span style="color: #000040;">*</span> timer<span style="color: #008000;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #0000ff;">class</span> CTimer <br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">public</span><span style="color: #008080;">:</span> &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">typedef</span> <span style="color: #0000ff;">enum</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TIMER_STOP&nbsp; <span style="color: #000080;">=</span> 0,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TIMER_RUN,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TIMER_PAUSE <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span> TimerState<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; CTimer<span style="color: #008000;">&#40;</span>CTimerCallbackInterface<span style="color: #000040;">*</span> callback,<span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">long</span> interval,<span style="color: #0000ff;">void</span><span style="color: #000040;">*</span> userData,<span style="color: #0000ff;">bool</span> repeat <span style="color: #000080;">=</span> <span style="color: #0000ff;">true</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">void</span> resume<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">void</span> pause<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">void</span> stop<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>&nbsp; &nbsp; <span style="color: #666666;">// calling this will destroy CTimer object</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">long</span> interval<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span> <span style="color: #0000ff;">return</span> m_ulInterval<span style="color: #008080;">;</span>&nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">void</span><span style="color: #000040;">*</span> userData<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span> <span style="color: #0000ff;">return</span> m_pUserData<span style="color: #008080;">;</span> &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">bool</span> isRepeat<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span> <span style="color: #0000ff;">return</span> m_bRepeat<span style="color: #008080;">;</span> &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; CTimerCallbackInterface<span style="color: #000040;">*</span> callback<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span> &nbsp; <span style="color: #008000;">&#123;</span> <span style="color: #0000ff;">return</span> m_pCallback<span style="color: #008080;">;</span> &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; TimerState state<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span> <span style="color: #0000ff;">return</span> m_state<span style="color: #008080;">;</span> &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #0000ff;">private</span><span style="color: #008080;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; CTimerCallbackInterface<span style="color: #000040;">*</span>&nbsp; &nbsp; m_pCallback<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">void</span><span style="color: #000040;">*</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; m_pUserData<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">bool</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; m_bRepeat<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">long</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; m_ulInterval<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; pthread_t &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; m_thread<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; TimerState&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; m_state<span style="color: #008080;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">friend</span> <span style="color: #0000ff;">void</span><span style="color: #000040;">*</span> onRun<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">void</span><span style="color: #000040;">*</span> ptr<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">void</span> start<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; ~CTimer<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span><br />
<br />
<span style="color: #008000;">&#125;</span><br />
<br />
<span style="color: #339900;">#endif // _IPHONEGEEK_UTILS_TIMER_H_</span></div></td></tr></tbody></table></div>
<p>&nbsp;</p>
<h2>timer.cpp:</h2>
<p>&nbsp;</p>
<div class="codecolorer-container cpp mac-classic" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br />61<br />62<br />63<br />64<br />65<br />66<br />67<br />68<br />69<br />70<br />71<br />72<br />73<br />74<br />75<br />76<br />77<br />78<br />79<br />80<br />81<br />82<br />83<br />84<br />85<br />86<br />87<br />88<br />89<br />90<br />91<br />92<br />93<br />94<br />95<br />96<br />97<br />98<br /></div></td><td><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #ff0000; font-style: italic;">/*<br />
&nbsp;* &nbsp;Timer.cpp<br />
&nbsp;* &nbsp;utils<br />
&nbsp;*<br />
&nbsp;* &nbsp;Created by Bagusflyer on 09-6-2.<br />
&nbsp;* &nbsp;Copyright 2009 www.iphone-geek.cn. All rights reserved.<br />
&nbsp;*<br />
&nbsp;*/</span><br />
<br />
<span style="color: #339900;">#include &quot;utils/timer.h&quot;</span><br />
<span style="color: #339900;">#include &lt;assert.h&gt;</span><br />
<br />
<span style="color: #0000ff;">namespace</span> utils<br />
<span style="color: #008000;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #666666;">// main loop for the timer thread</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">void</span><span style="color: #000040;">*</span> onRun<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">void</span><span style="color: #000040;">*</span> ptr<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span> &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">bool</span> run <span style="color: #000080;">=</span> <span style="color: #0000ff;">true</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; CTimer<span style="color: #000040;">*</span> theTimer <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span>CTimer<span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span>ptr<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">while</span><span style="color: #008000;">&#40;</span> run <span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">switch</span> <span style="color: #008000;">&#40;</span> theTimer<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>state<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">case</span> CTimer<span style="color: #008080;">::</span><span style="color: #007788;">TIMER_PAUSE</span><span style="color: #008080;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; msleep<span style="color: #008000;">&#40;</span>50<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">break</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">case</span> CTimer<span style="color: #008080;">::</span><span style="color: #007788;">TIMER_RUN</span><span style="color: #008080;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CTimerCallbackInterface<span style="color: #000040;">*</span> callback <span style="color: #000080;">=</span> theTimer<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>callback<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; callback<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>onTimer<span style="color: #008000;">&#40;</span>theTimer<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span> <span style="color: #000040;">!</span>theTimer<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>isRepeat<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; theTimer<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>stop<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; msleep<span style="color: #008000;">&#40;</span>theTimer<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>interval<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">break</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">case</span> CTimer<span style="color: #008080;">::</span><span style="color: #007788;">TIMER_STOP</span><span style="color: #008080;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; run <span style="color: #000080;">=</span> <span style="color: #0000ff;">false</span><span style="color: #008080;">;</span>&nbsp; &nbsp; <span style="color: #666666;">// exit the main loop</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">break</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000dd;">delete</span> theTimer<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">NULL</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; &nbsp; CTimer<span style="color: #008080;">::</span><span style="color: #007788;">CTimer</span><span style="color: #008000;">&#40;</span>CTimerCallbackInterface<span style="color: #000040;">*</span> callback,<span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">long</span> interval,<span style="color: #0000ff;">void</span><span style="color: #000040;">*</span> userData,<span style="color: #0000ff;">bool</span> repeat<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080;">:</span> m_pCallback<span style="color: #008000;">&#40;</span>callback<span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; m_pUserData<span style="color: #008000;">&#40;</span>userData<span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; m_bRepeat<span style="color: #008000;">&#40;</span>repeat<span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; m_ulInterval<span style="color: #008000;">&#40;</span>interval<span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; m_state<span style="color: #008000;">&#40;</span>TIMER_PAUSE<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000dd;">assert</span><span style="color: #008000;">&#40;</span>callback<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666;">// create a thread</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; pthread_create<span style="color: #008000;">&#40;</span> <span style="color: #000040;">&amp;</span>m_thread,<span style="color: #0000ff;">NULL</span>,onRun, <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">void</span> <span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span><span style="color: #0000dd;">this</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666;">// TODO: error check</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; start<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; &nbsp; CTimer<span style="color: #008080;">::</span>~CTimer<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; pthread_join<span style="color: #008000;">&#40;</span>m_thread, <span style="color: #0000ff;">NULL</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #0000ff;">void</span> CTimer<span style="color: #008080;">::</span><span style="color: #007788;">start</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; m_state <span style="color: #000080;">=</span> TIMER_RUN<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #0000ff;">void</span> CTimer<span style="color: #008080;">::</span><span style="color: #007788;">resume</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666;">// TODO: to be done</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; m_state <span style="color: #000080;">=</span> TIMER_RUN<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #0000ff;">void</span> CTimer<span style="color: #008080;">::</span><span style="color: #007788;">stop</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; m_state <span style="color: #000080;">=</span> TIMER_STOP<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #0000ff;">void</span> CTimer<span style="color: #008080;">::</span><span style="color: #007788;">pause</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666;">// TODO: to be implemented</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; m_state <span style="color: #000080;">=</span> TIMER_PAUSE<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
<br />
<span style="color: #008000;">&#125;</span></div></td></tr></tbody></table></div>
<p>&nbsp;</p>
<p>注意：msleep在<a href="%E7%BC%96%E7%A8%8B/%E8%B7%A8%E5%B9%B3%E5%8F%B0%E4%BB%A3%E7%A0%81%E5%88%86%E4%BA%AB-%E7%BA%BF%E7%A8%8B">跨平台代码分享系列之一</a>中有说明。</p>
<p>&nbsp;</p>
<h2>使用方法:</h2>
<p>&nbsp;</p>
<div class="codecolorer-container cpp mac-classic" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br /></div></td><td><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666;">// .h文件中</span><br />
<span style="color: #0000ff;">class</span> MyApp<span style="color: #008080;">:</span> <span style="color: #0000ff;">public</span> utils<span style="color: #008080;">::</span><span style="color: #007788;">CTimerCallbackInterface</span> &nbsp;<span style="color: #666666;">// 继承CTimerCallbackInterface</span><br />
<span style="color: #008000;">&#123;</span><br />
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">void</span> onTimer<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> utils<span style="color: #008080;">::</span><span style="color: #007788;">CTimer</span><span style="color: #000040;">*</span> timer<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// 实现onTimer</span><br />
<br />
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span><br />
<br />
<span style="color: #666666;">// .cpp 文件中某处</span><br />
<span style="color: #666666;">// 参数1 - 指向CTimerCallbackInterface</span><br />
<span style="color: #666666;">// 参数2 - 定时器分辨率，以ms为单位，此例为10ms</span><br />
<span style="color: #666666;">// 参数3 - 数据指针，用来传递数据到定时器回调中</span><br />
<span style="color: #666666;">// 参数4 - 是否循环触发</span><br />
utils<span style="color: #008080;">::</span><span style="color: #007788;">CTimer</span><span style="color: #000040;">*</span> timer <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> utils<span style="color: #008080;">::</span><span style="color: #007788;">CTimer</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">this</span>,10,data,<span style="color: #0000ff;">true</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <br />
<br />
<span style="color: #666666;">// 回调</span><br />
<span style="color: #0000ff;">void</span> MyApp<span style="color: #008080;">::</span><span style="color: #007788;">onTimer</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> utils<span style="color: #008080;">::</span><span style="color: #007788;">CTimer</span><span style="color: #000040;">*</span> theTimer<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #666666;">// 指向你想要的功能</span><br />
&nbsp; &nbsp; <span style="color: #666666;">// 你可以使用下面方法获取来自于定时器的数据</span><br />
&nbsp; &nbsp; UserData<span style="color: #000040;">*</span> data <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span>UserData<span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span>theTimer<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>userData<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<span style="color: #008000;">&#125;</span><br />
<br />
<span style="color: #666666;">// 释放</span><br />
<span style="color: #666666;">// 记住一定要调用stop，来删除创建的定时器</span><br />
timer<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>stop<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></div></td></tr></tbody></table></div>
<p>&nbsp;</p>
<p>下次的题目是跨平台xml解析。有什么问题，建议，希望大家给点回复！你们的支持是我坚持下去的动力，谢谢！</p>
]]></content:encoded>
			<wfw:commentRss>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/%e8%b7%a8%e5%b9%b3%e5%8f%b0%e4%bb%a3%e7%a0%81%e5%88%86%e4%ba%ab%e4%b9%8b%e4%ba%8c-%e5%ae%9a%e6%97%b6%e5%99%a8/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>代码段 – NSTimer示例</title>
		<link>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/%e4%bb%a3%e7%a0%81%e6%ae%b5-%e2%80%93-nstimer%e7%a4%ba%e4%be%8b</link>
		<comments>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/%e4%bb%a3%e7%a0%81%e6%ae%b5-%e2%80%93-nstimer%e7%a4%ba%e4%be%8b#comments</comments>
		<pubDate>Thu, 23 Jul 2009 12:57:35 +0000</pubDate>
		<dc:creator>bagusflyer</dc:creator>
				<category><![CDATA[编程]]></category>
		<category><![CDATA[定时器]]></category>
		<category><![CDATA[代码片段]]></category>

		<guid isPermaLink="false">http://iphonegeekbeta.zuesitech.com/?p=104</guid>
		<description><![CDATA[简介 
&#160;
今天我们带来的是一个有关NSTimer的小项目。我们将要建立一个赛马的应用程序。我们将在屏幕下方创建一个由6个带有蓝色背景色的小UIView方框组成的视图，然后使用定时器随意移动其中一个任意的距离。开动！



&#160;
源代码 

获取源代码：NSTimerDemo
&#160;
步骤 
步骤1：
用Xcode建立一个view-based应用程序，随便取一个名。

步骤2：

在视图控制器的头文件中加入：
1NSTimer *myTimer;
步骤3：

在视图控制器类中，取消viewDidLoad方法的注解并写入以下代码：
123456789101112131415161718192021222324- &#40;void&#41;viewDidLoad
&#123;
&#160; &#160; &#91;super viewDidLoad&#93;;
&#160; &#160; CGRect workingFrame;

&#160; &#160; workingFrame.origin.x = 15;
&#160; &#160; workingFrame.origin.y = 400;
&#160; &#160; workingFrame.size.width = 40;
&#160; &#160; workingFrame.size.height = 40;

&#160; &#160; for&#40;int i = 0; i &#38;lt; 6; i++&#41;
&#160; &#160; &#123;
&#160; &#160; &#160; &#160; UIView *myView = &#91;&#91;UIView alloc&#93; &#160;initWithFrame:workingFrame&#93;;
&#160; &#160; &#160; &#160; &#91;myView setTag:i&#93;;

&#160; &#160; &#160; [...]]]></description>
			<content:encoded><![CDATA[<h2 class="weebly-menu-more"><strong>简介 </strong></h2>
<p>&nbsp;</p>
<p align="left">今天我们带来的是一个有关NSTimer的小项目。我们将要建立一个赛马的应用程序。我们将在屏幕下方创建一个由6个带有蓝色背景色的小UIView方框组成的视图，然后使用定时器随意移动其中一个任意的距离。开动！</p>
<p align="left">
<p><span id="more-104"></span><br />
<img src="http://icodeblog.com/wp-content/uploads/2009/07/Picture-16.png" border="0" alt="Picture 16" width="351" height="693" /></p>
<p>&nbsp;</p>
<h2><strong>源代码 </strong></h2>
<p align="left">
<p>获取源代码：<a href="http://icodeblog.com/wp-content/uploads/2009/07/NSTimerDemo.zip">NSTimerDemo</a></p>
<p align="left">&nbsp;</p>
<h2><strong>步骤 </strong></h2>
<h3>步骤1：</h3>
<p align="left">用Xcode建立一个view-based应用程序，随便取一个名。</p>
<p align="left">
<h3>步骤2：</h3>
<p align="left">
<p align="left">在视图控制器的头文件中加入：</p>
<div class="codecolorer-container objc mac-classic" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSTimer_Class/"><span style="color: #400080;">NSTimer</span></a> <span style="color: #002200;">*</span>myTimer;</div></td></tr></tbody></table></div>
<h3>步骤3：</h3>
<p align="left">
<p>在视图控制器类中，取消viewDidLoad方法的注解并写入以下代码：</p>
<div class="codecolorer-container objc mac-classic" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br /></div></td><td><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>viewDidLoad<br />
<span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #002200;">&#91;</span>super viewDidLoad<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; CGRect workingFrame;<br />
<br />
&nbsp; &nbsp; workingFrame.origin.x <span style="color: #002200;">=</span> <span style="color: #2400d9;">15</span>;<br />
&nbsp; &nbsp; workingFrame.origin.y <span style="color: #002200;">=</span> <span style="color: #2400d9;">400</span>;<br />
&nbsp; &nbsp; workingFrame.size.width <span style="color: #002200;">=</span> <span style="color: #2400d9;">40</span>;<br />
&nbsp; &nbsp; workingFrame.size.height <span style="color: #002200;">=</span> <span style="color: #2400d9;">40</span>;<br />
<br />
&nbsp; &nbsp; <span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span> i <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>; i <span style="color: #002200;">&amp;</span>lt; <span style="color: #2400d9;">6</span>; i<span style="color: #002200;">++</span><span style="color: #002200;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; UIView <span style="color: #002200;">*</span>myView <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIView alloc<span style="color: #002200;">&#93;</span> &nbsp;initWithFrame<span style="color: #002200;">:</span>workingFrame<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #002200;">&#91;</span>myView setTag<span style="color: #002200;">:</span>i<span style="color: #002200;">&#93;</span>;<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #002200;">&#91;</span>myView setBackgroundColor<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>UIColor &nbsp;blueColor<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; workingFrame.origin.x <span style="color: #002200;">=</span> &nbsp;workingFrame.origin.x <span style="color: #002200;">+</span> workingFrame.size.width <span style="color: #002200;">+</span> <span style="color: #2400d9;">10</span>;<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #002200;">&#91;</span>self.view addSubview<span style="color: #002200;">:</span>myView<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; <span style="color: #002200;">&#125;</span><br />
<br />
&nbsp; &nbsp; myTimer <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSTimer_Class/"><span style="color: #400080;">NSTimer</span></a> &nbsp;scheduledTimerWithTimeInterval<span style="color: #002200;">:</span>.1 target<span style="color: #002200;">:</span>self selector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>moveACar<span style="color: #002200;">&#41;</span> &nbsp; &nbsp; userInfo<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span> repeats<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;<br />
<br />
<span style="color: #002200;">&#125;</span></div></td></tr></tbody></table></div>
<p>&nbsp;</p>
<h3>步骤4：</h3>
<p>在视图控制器类中加入下列方法：</p>
<div class="codecolorer-container objc mac-classic" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br /></div></td><td><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>viewDidLoad<br />
<span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #002200;">&#91;</span>super viewDidLoad<span style="color: #002200;">&#93;</span>;<br />
<br />
&nbsp; &nbsp; CGRect workingFrame;<br />
&nbsp; &nbsp; workingFrame.origin.x <span style="color: #002200;">=</span> <span style="color: #2400d9;">15</span>;<br />
&nbsp; &nbsp; workingFrame.origin.y <span style="color: #002200;">=</span> <span style="color: #2400d9;">400</span>;<br />
&nbsp; &nbsp; workingFrame.size.width <span style="color: #002200;">=</span> <span style="color: #2400d9;">40</span>;<br />
&nbsp; &nbsp; workingFrame.size.height <span style="color: #002200;">=</span> <span style="color: #2400d9;">40</span>;<br />
<br />
&nbsp; &nbsp; <span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span> i <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>; i <span style="color: #002200;">&amp;</span>lt; <span style="color: #2400d9;">6</span>; i<span style="color: #002200;">++</span><span style="color: #002200;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; UIView <span style="color: #002200;">*</span>myView <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIView alloc<span style="color: #002200;">&#93;</span> initWithFrame<span style="color: #002200;">:</span>workingFrame<span style="color: #002200;">&#93;</span>;<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #002200;">&#91;</span>myView setTag<span style="color: #002200;">:</span>i<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #002200;">&#91;</span>myView setBackgroundColor<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>UIColor blueColor<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; workingFrame.origin.x <span style="color: #002200;">=</span> workingFrame.origin.x <span style="color: #002200;">+</span> workingFrame.size.width <span style="color: #002200;">+</span> <span style="color: #2400d9;">10</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #002200;">&#91;</span>self.view addSubview<span style="color: #002200;">:</span>myView<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; <span style="color: #002200;">&#125;</span><br />
<br />
&nbsp; &nbsp; myTimer <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSTimer_Class/"><span style="color: #400080;">NSTimer</span></a> scheduledTimerWithTimeInterval<span style="color: #002200;">:</span>.1 target<span style="color: #002200;">:</span>self selector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>moveACar<span style="color: #002200;">&#41;</span> &nbsp;userInfo<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span> repeats<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;<br />
<br />
<span style="color: #002200;">&#125;</span></div></td></tr></tbody></table></div>
<p>&nbsp;</p>
<h2><strong>结论 </strong></h2>
<p>对于各种项目中的小问题，定时器是一个十分方便的解决方案。</p>
<p>&nbsp;</p>
<h5><strong>23/07/2009 </strong><strong>原文：</strong><a href="http://icodeblog.com/2009/07/23/nstimer-the-poor-mans-threading-code-snapshot/"><strong>The Poor Man’s Threading</strong></a><strong> </strong></h5>
]]></content:encoded>
			<wfw:commentRss>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/%e4%bb%a3%e7%a0%81%e6%ae%b5-%e2%80%93-nstimer%e7%a4%ba%e4%be%8b/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

