Original URL: http://my.oschina.net/u/2340880/blog/398598
Nstimer is often used in iOS development, especially in small games, but for beginners, the memory release problem is often not noticed, and its basic usage is summarized as follows:
First, initialization method: There are five kinds of initialization methods, respectively, are
+ (Nstimer *) Timerwithtimeinterval: (nstimeinterval) ti invocation: (nsinvocation *) invocation repeats: (BOOL) Yesorno;
- (void) Viewdidload {[Super Viewdidload];Initializes a invocation objectNsinvocation * Invo = [Nsinvocation invocationwithmethodsignature:[[self class] instancemethodsignatureforselector: @selector (init)]; [invo settarget:self]; [invo setselector: @selector (MyLog)]; nstimer * timer = [NSTimer Timerwithtimeinterval:1 invocation:invo repeats: yes]; //join the main circulation pool [[nsrunloop mainrunloop]addtimer:timer formode: nsdefaultrunloopmode]; //start loop [timer fire];}
+ (Nstimer *) Scheduledtimerwithtimeinterval: (nstimeinterval) ti invocation: (nsinvocation *) invocation repeats: (BOOL) Yesorno;
NSTimer * timer = [NSTimer scheduledTimerWithTimeInterval:1 invocation:invo repeats:YES];
+ (Nstimer *) Timerwithtimeinterval: (nstimeinterval) TI target: (ID) atarget selector: (SEL) Aselector userInfo: (ID) UserInfo repeats: (BOOL) Yesorno;
NSTimer * timer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(myLog) userInfo:nil repeats:NO]
+ (Nstimer *) Scheduledtimerwithtimeinterval: (nstimeinterval) TI target: (ID) atarget selector: (SEL) Aselector UserInfo :(ID) UserInfo repeats: (BOOL) Yesorno;
NSTimer * timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(myLog:) userInfo:@"123" repeats:YES]
-(Instancetype) Initwithfiredate: (nsdate *) date interval: (nstimeinterval) TI target: (ID) T selector: (SEL) s UserInfo: ( ID) UI repeats: (BOOL) Rep
NSTimer * timer = [[NSTimer alloc]initWithFireDate:[NSDate distantPast] interval:1 target:self selector:@selector(myLog:) userInfo:nil repeats:YES]; [[NSRunLoop mainRunLoop]addTimer:timer forMode:NSDefaultRunLoopMode];
Note: The similarities and differences between the five initialization methods:
1, the parameter repeats is to specify whether to loop execution, yes will loop, no will be executed only once.
2. Timerwithtimeinterval the objects created by these two class methods will not be executed without the Addtimer:formode method being manually added to the main loop pool. And if you do not call fair manually, the timer does not start.
3, Scheduledtimerwithtimeinterval These two methods do not need to manually call fair, will automatically execute, and automatically join the main loop pool.
4, the Init method needs to be manually added to the loop pool, it will start at the set start time.
Second, member variables
@property (copy) NSDate *firedate;
This is to set the timer start time, commonly used to manage the timer start and stop
//启动定时器 timer.fireDate = [NSDate distantPast]; //停止定时器 timer.fireDate = [NSDate distantFuture];
@property (readonly) Nstimeinterval timeinterval;
This is a read-only property that gets the timer call interval.
@property nstimeinterval Tolerance;
This is a new attribute after 7.0, because the Nstimer is not completely accurate and sets the error range by this value.
@property (readonly, getter=isvalid) BOOL valid;
Gets whether the timer is valid
@property (ReadOnly, retain) ID userInfo;
Get parameter information
Third, about memory release
If we start a timer, the timer is stopped, or even nil, before a certain interface is released, because the object is still in the loop pool of the system. So we need to do this:
-(void) dealloc{NSLog (@ "dealloc:%@", [Self class]);} - (void) Viewdidload {[Super Viewdidload]; timer= [Nstimer Scheduledtimerwithtimeinterval:1 target:Self selector:@selector (myLog:) UserInfo:Nil repeats:YES];UIButton *btn = [[UIButton Alloc]initwithframe:CGRectMake (0,0,100, 100)]; Btn.backgroundcolor=[uicolor redcolor]; [btn Addtarget:self action: @selector (BTN) Forcontrolevents:uicontroleventtouchupinside]; [self.view addsubview:btn];} -(void) btn{ if ( Timer.isvalid) { [timer invalidate]; } timer=nil; [self dismissviewcontrolleranimated:YES completion:nil];}
In the official documentation we can see that [timer invalidate] is the only way to remove the timer from the loop pool.
"Go" IOS Nstimer Timer Usage Summary