Original URL: http://blog.csdn.net/tangshoulin/article/details/7644124
1. Initialization
+ (Nstimer *) Timerwithtimeinterval: (nstimeinterval) TI target: (ID) atarget selector: (SEL) Aselector userInfo: (ID) UserInfo repeats: (BOOL) Yesorno;
+ (Nstimer *) Scheduledtimerwithtimeinterval: (nstimeinterval) TI target: (ID) atarget selector: (SEL) Aselector UserInfo :(ID) UserInfo repeats: (BOOL) Yesorno;
Note: Without scheduled initialization, manual Addtimer:formode is required: Add the timer to a runloop.
The scheduled initialization method will be added directly to the current runloop with the default mode.
Example:
Nstimer *timer = [Nstimer scheduledtimerwithtimeinterval:10.0 target:self selector: @selector (timerfired:) UserInfo: Nil Repeats:no];
Or
Nstimer *MyTimer = [nstimertimerwithtimeinterval:3.0 target:selfselector:@ Selector(timerfired:) userInfo:nilrepeats:NO];
[[nsrunloopcurrentrunloop] addtimer:mytimerformode:nsdefaultrunloopmode];
2. Trigger (START)
When the timer is created (not scheduled, added to Runloop, the timer is automatically triggered after the timeinterval seconds specified at initialization.
You can use the-(void) Fire method to trigger the timer immediately;
Note: You can use the This method to fire a repeating timer without interrupting its regular firing schedule. If the timer is non-repeating, it's automatically invalidated after firing, even if its scheduled fire date have not Arriv Ed.
The timer is triggered immediately after calling this method in a repeating timer, but does not interrupt its previous execution plan;
Calling this method in a timer that does not execute repeatedly will invalidate the timer immediately after it is triggered.
3. Stop
-(void) invalidate;
This is the only way to move the timer out of the Runloop.
Note:
The Nstimer can be accurate to 50-100 milliseconds.
The Nstimer is not absolutely accurate, and the intermediate time-consuming or blocking misses the next point, then the next point passes.
Use code Examples:
The timer that works well in the game Loop-(void) starttimer{//repeats is set to Yes when re-executing after each invalidate, and if no, the timer is not valid after the logic executes [nstimer scheuledtimerwithtimeinterval:1.0/60.0 Target:view selector:@ (timeradvanced:) Userinfo:nil repeats:yes];} -(void) timeradvanced: (Nstimer *) timer//This function will execute a loop of logic {//plus a counter int mTime; if (MTime = = 0) {[timer invalidate]; [Self starttimer]; } mtime++; if (mTime >= a maximum value maxcount)//Handle some logic ..... Reset Mtime to 0} somewhere
"Go" IOS timer Nstimer