NSTimer總結

+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti invocation:(NSInvocation *)invocation repeats:(BOOL)yesOrNo;

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

//若date爲 [NSDate distantPast] 馬上啓動 若爲[NSDate distantFuture] 直到調用fire才啓動

//需要  [[NSRunLoop mainRunLoop]addTimer:self.timer forMode:NSDefaultRunLoopMode];

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

//需要  [[NSRunLoop mainRunLoop]addTimer:self.timer forMode:NSDefaultRunLoopMode];

//加入即運行,通過fireDate進行暫停、開啓操作

demo:

    self.timer = [NSTimer timerWithTimeInterval:2.0 target:self selector:@selector(printTimer) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop]addTimer:self.timer forMode:NSDefaultRunLoopMode];

+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(nullable id)userInfo repeats:(BOOL)yesOrNo;

//不需要顯示加入主循環

+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti invocation:(NSInvocation *)invocation repeats:(BOOL)yesOrNo;

//不需要顯示加入主循環

- (void)fire;
//啓動

@property (copy) NSDate *fireDate;

//定時器的啓動時間 1、[NSDate distantPast] 馬上啓動;2、 [NSDate distantFuture]永不啓動,可用於暫停。

@property (readonly) NSTimeInterval timeInterval; //獲取時間間隔

- (void)invalidate;  //移除定時器,無法恢復

發佈了101 篇原創文章 · 獲贊 7 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章