NSTimer使用

作者: Stubborn_强 | 来源:发表于2016-08-25 11:50 被阅读28次

1、初始化

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

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

注意:userInfo是指NSTimer携带的用户信息。

不用scheduled方式初始化的,需要手动addTimer:forMode: 将timer添加到一个runloop中。

而scheduled的初始化方法将以默认mode直接添加到当前的runloop中.

例子:

[NSTimer scheduledTimerWithTimeInterval:2 target:selfselector:@selector(startFindApartment:) userInfo:nil repeats:YES];

NSTimer *myTimer = [NSTimertimerWithTimeInterval:3.0 target:selfselector:@selector(timerFired:) userInfo:nilrepeats:NO];

[[NSRunLoopcurrentRunLoop] addTimer:myTimerforMode:NSDefaultRunLoopMode];

2、触发(启动)

当定时器创建完(不用scheduled的,添加到runloop中后,该定时器将在初始化时指定的timeInterval秒后自动触发。

NSTimer*timer=[NSTimertimerWithTimeInterval:0.5target:selfselector:@selector(timeSchedule)userInfo:nilrepeats:YES];

NSRunLoop*runLoop=[NSRunLoopcurrentRunLoop];

[runLoopaddTimer:timerforMode:NSDefaultRunLoopMode];

[timerfire];

可以使用-(void)fire;方法来立即触发该定时器;

3、停止

- (void)invalidate;

这个是唯一一个可以将计时器从runloop中移出的方法。

4、在多线程开发中,如果是在mainthread中使用定时器,两种初始化方法都能使用,如果是在子线程中使用定时器,只能使用方法:

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

并且启动定时器不能用fire,只能让runloop一直执行下去,sample code:

1、

_timer=[NSTimertimerWithTimeInterval:0.5target:selfselector:@selector(timeSchedule)userInfo:nilrepeats:YES];

NSRunLoop*runLoop=[NSRunLoopcurrentRunLoop];

[runLoopaddTimer:_timerforMode:NSDefaultRunLoopMode];

while([runLooprunMode:NSDefaultRunLoopModebeforeDate:[NSDatedistantFuture]]);

2、

self.timer = [NSTimer timerWithTimeInterval:2 target:self selector:@selector(gainACSessionId) userInfo:nil repeats:YES];

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

定时器需要加入到mainrunloop里才不会在拖拽界面时或者其他操作时停止timer,默认是添加到currentrunloop,需要改变其mode,把NSDefaultRunLoopMode改成NSRunLoopCommonModes

例子:

self.timer = [NSTimer timerWithTimeInterval:2 target:self selector:@selector(gainSessionId) userInfo:nil repeats:YES];

[[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];

相关文章

  • NSTimer的循环引用

    NSTimer基本使用 NSTimer与RunLoop NSTimer 循环引用的问题 如何在子线程使用NSTim...

  • iOS-NSTimer-循环引用问题

    在使用NSTimer的时候,NSTimer会生成指向其使用者的引用,而其使用者如果也引用了NSTimer,那么就会...

  • NSTimer的使用

    NSTimer 的使用 为什么会写NSTimer呢? 原因很简单, 这里有坑! NSTimer 使用的顺序 创建N...

  • 内存管理总结

    CADisplayLink、NSTimer使用注意 CADisplayLink、NSTimer会对target产生...

  • Objective-C基础-内存管理

    1、CADisplayLink、NSTimer使用 CADisplayLink、NSTimer会对target产生...

  • CADisplayLink、NSTimer使用注意

    CADisplayLink、NSTimer使用注意 CADisplayLink、NSTimer会对target产生...

  • 底层-内存管理

    CADisplayLink、NSTimer使用注意 CADisplayLink、NSTimer会对target产生...

  • 内存管理

    CADisplayLink、NSTimer使用注意 CADisplayLink、NSTimer会对target产生...

  • iOS内存管理

    CADisplayLink、NSTimer使用注意 CADisplayLink、NSTimer会对target产生...

  • 内存管理

    CADisplayLink、NSTimer使用注意 CADisplayLink、NSTimer会对target产生...

网友评论

    本文标题:NSTimer使用

    本文链接:https://www.haomeiwen.com/subject/scyocttx.html