Nstimer usage, pause, continue, initialize
Reprint: http://blog.csdn.net/zhuzhihai1988/article/details/7742881
How to use Nstimer
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.
Scheduledtimerwithtimeinterval: (nstimeinterval) seconds
Book a timer and set a time interval.
Represents a value that enters a time interval object, in seconds, of a >0 floating-point type, and if the value is <0, the system defaults to 0.1
Target: (ID) atarget
Represents the Sent object, such as self
Selector: (SEL) Aselector
Method selector, which, within the time interval, chooses to invoke an instance method
UserInfo: (ID) userInfo
This parameter can be nil, and the timer is retained and freed by the object you specify when the timer fails.
Repeats: (BOOL) Yesorno
When yes, the timer loops continuously until it expires or is released, and when no, the timer is sent out once and then fails.
Invocation: (nsinvocation *) invocation
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.
Add a category to Nstimer
#import "TFTimer.h"
#import <Foundation/Foundation.h>
@interface Nstimer (tfaddition)
-(void) Pausetimer;
-(void) Resumetimer;
@end
#import "TFTimer.h"
@implementation Nstimer (tfaddition)
-(void) pausetimer{
if (![ Self IsValid]) {
return;
}
[Self setfiredate:[nsdate distantfuture]]; If you give me a deadline, I hope it's 4001-01-01 00:00:00 +0000
}
-(void) resumetimer{
if (![ Self IsValid]) {
return;
}
[Self setfiredate:[nsdate datewithtimeintervalsincenow:0]];
[Self setfiredate:[nsdate date]];
}
@end
Nstimer usage, pause, continue, initialize