iOS定時器

相關類:NSTimerhtml

A timer is not a real-time mechanism; it fires only when one of the run loop modes to which the timer has been added is running and able to check if the timer’s firing time has passed. Because of the various input sources a typical run loop manages, the effective resolution of the time interval for a timer is limited to on the order of 50-100 milliseconds. If a timer’s firing time occurs during a long callout or while the run loop is in a mode that is not monitoring the timer, the timer does not fire until the next time the run loop checks the timer.ios

Timer不是實時的,依賴於運行的loop,精度爲50-100ms。若是run loop沒有monitoring timer或者正在long callout,Timer不會被觸發。api

建立定時器

A timer object can be registered in only one run loop at a time, although it can be added to multiple run loop modes within that run loop. There are three ways to create a timer:scheduledTimerWithTimeInterval、timerWithTimeInterval和initWithFireDateapp

後二者在建立以後須要調用addTimer:forMode:oop

Once scheduled on a run loop, the timer fires at the specified interval until it is invalidated. A non-repeating timer invalidates itself immediately after it fires.you should always call the invalidate method from the same thread on which the timer was installed. Invalidating the timer immediately disables it so that it no longer affects the run loop.this

 

NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:100 target:self selector:@selector(timerFireMethod:) userInfo:nil repeats:YES];spa

userInfo:The user info for the timer. The timer maintains a strong reference to this object until it (the timer) is invalidated. This parameter may be nil.3d

 

selector原型: -(void)timerFireMethod:(NSTimer*)timercode

啓停

暫停:[timer setFireDate:[NSDate distantFuture]]htm

恢復:[timer setFireDate:[NSDate date]]

相關文章
相關標籤/搜索