Three ways to create a timer object
First, these two class methods create aTimerand assign it to a defaultRunloopMode + (Nstimer*)Scheduledtimerwithtimeinterval:(nstimeinterval) ti invocation:(nsinvocation*)invocation repeats:(BOOL) Yesorno;+ (Nstimer*)Scheduledtimerwithtimeinterval:(nstimeinterval) ti Target:(ID) atarget selector:(SEL) aselector UserInfo:(ID) userInfo repeats:(BOOL) YesornoTwo, these two class methods create aTimerobject, not assigning it to whichRunloop. (when created, you must manually call theNsrunloopThe corresponding method under theAddTimer: Formode: Go to assign it to aRunloopmode.) + (Nstimer*)Timerwithtimeinterval:(nstimeinterval) ti Target:(ID) atarget selector:(SEL) aselector UserInfo:(ID) userInfo repeats:(BOOL) Yesorno;+ (Nstimer*)Timerwithtimeinterval:(nstimeinterval) ti invocation:(nsinvocation*)invocation repeats:(BOOL) Yesorno; third, create aNstimer(when created, you must manually call theNsrunloopThe corresponding method under theAddTimer: Formode: Go to assign it to aRunloopmode.)-(Instancetype)initwithfiredate:(NSDate*)Date interval:(nstimeinterval) ti Target:(ID) T selector:(SEL) s UserInfo:(ID) UI repeats:(BOOL) rep-
Typically used
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo;TimeInterval:时间间隔,浮点型的值。小于0时系统默认为0.1。用于指定没间隔该时间段触发一次指定的方法target:方法作用对象 比如self selector:作用方法userInfo:当定时器失效时,由你指定的对象保留和释放该定时器。通常为nilrepeats:YES时,不断循环直至计时器失效或被释放。NO时只循环一次就失效
The rest of the methods introduce
- (void)fire;立即触发计时器- (void)invalidate;停止计时并使触发器失效- (BOOL)isValid;判断触发器是否有效- (NSTimeInterval)timeInterval;触发间隔时间- (id)userInfo;一般为nil- (NSDate *)fireDate;//开始时间- (void)setFireDate:(NSDate *)date;//设置fireData,其实暂停、开始会用到
A brief description of the timer pause and start
[timer setFireDate:[NSDate distantPast]];//开启[timer setFireDate:[NSDate//继续。[timer setFireDate:[NSDate distantFuture]];//暂停暂停之后计时器仍然有效,而如果用invalidate则会失效
Before invalidate, it is better to use IsValid first to determine whether or not in the thread
if ([timer isValid] == YES) { [timer invalidate]; timer = nil;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
iOS Development-nstimer