012-NSTimer

一、初始化方法

1.需要调用addTimer: forMode方法手动加入主循环池中;并且手动调用fire,开始循环

+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti invocation:(NSInvocation *)invocation repeats:(BOOL)yesOrNo;
+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo;

2.自动加入主循环池中;并且自动开始循环

+ (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)yesOrNo;

3.需要调用addTimer: forMode方法手动加入主循环池中;不需要手动调用fire,它会在设定时间启动循环

- (instancetype)initWithFireDate:(NSDate *)date interval:(NSTimeInterval)ti target:(id)t selector:(SEL)s userInfo:(id)ui repeats:(BOOL)rep;

附:repeats:是否循环执行(YES-循环执行,NO-仅执行一次)

 

二、属性&销毁定时器

1.这是设置定时器的启动时间,常用来管理定时器的启动与停止

@property (copy) NSDate *fireDate;

1)启动定时器

timer.fireDate = [NSDate distantPast];

2)停止定时器

timer.fireDate = [NSDate distantFuture];

3)销毁定时器

[timer invalidate];

timer = nil;

2. 这个是一个只读属性,获取定时器调用间隔时间

@property (readonly) NSTimeInterval timeInterval;

3.这是7.0之后新增的一个属性,因为NSTimer并不完全精准,通过这个值设置误差范围

@property NSTimeInterval tolerance;

4.获取定时器是否有效

@property (readonly, getter=isValid) BOOL valid;

5.定时器循环执行时的传参

@property (readonly, retain) id userInfo;

 

posted @ 2017-11-16 09:27  Frank9098  阅读(106)  评论(0)    收藏  举报