iOS NSTimer 定時(shí)器用法總結(jié)

NSTimer在IOS開發(fā)中會(huì)經(jīng)常用到,尤其是小型游戲,然而對(duì)于初學(xué)者時(shí)常會(huì)注意不到其中的內(nèi)存釋放問題,將其基本用法總結(jié)如下:

一、初始化方法:有五種初始化方法,分別是

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

- (void)viewDidLoad {

[super viewDidLoad];

//初始化一個(gè)Invocation對(duì)象

NSInvocation * invo = [NSInvocation invocationWithMethodSignature:[[self class] instanceMethodSignatureForSelector:@selector(init)]];

[invo setTarget:self];

[invo setSelector:@selector(myLog)];

NSTimer * timer = [NSTimer timerWithTimeInterval:1 invocation:invo repeats:YES];

//加入主循環(huán)池中

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

//開始循環(huán)

[timer fire];

}

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

NSTimer * timer = [NSTimer scheduledTimerWithTimeInterval:1 invocation:invo repeats:YES];

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

NSTimer * timer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(myLog) userInfo:nil repeats:NO]

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

NSTimer * timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(myLog:) userInfo:@"123" repeats:YES]

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

NSTimer * timer = [[NSTimer alloc]initWithFireDate:[NSDate distantPast] interval:1 target:self selector:@selector(myLog:) userInfo:nil repeats:YES];

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

注意:這五種初始化方法的異同:

1、參數(shù)repeats是指定是否循環(huán)執(zhí)行,YES將循環(huán),NO將只執(zhí)行一次。

2、timerWithTimeInterval這兩個(gè)類方法創(chuàng)建出來的對(duì)象如果不用 addTimer: forMode方法手動(dòng)加入主循環(huán)池中,將不會(huì)循環(huán)執(zhí)行。并且如果不手動(dòng)調(diào)用fair,則定時(shí)器不會(huì)啟動(dòng)。

3、scheduledTimerWithTimeInterval這兩個(gè)方法不需要手動(dòng)調(diào)用fair,會(huì)自動(dòng)執(zhí)行,并且自動(dòng)加入主循環(huán)池。

4、init方法需要手動(dòng)加入循環(huán)池,它會(huì)在設(shè)定的啟動(dòng)時(shí)間啟動(dòng)。

二、成員變量

@property (copy) NSDate *fireDate;

這是設(shè)置定時(shí)器的啟動(dòng)時(shí)間,常用來管理定時(shí)器的啟動(dòng)與停止

//啟動(dòng)定時(shí)器

timer.fireDate = [NSDate distantPast];

//停止定時(shí)器

timer.fireDate = [NSDate distantFuture];

@property (readonly) NSTimeInterval timeInterval;

這個(gè)是一個(gè)只讀屬性,獲取定時(shí)器調(diào)用間隔時(shí)間。

@property NSTimeInterval tolerance;

這是7.0之后新增的一個(gè)屬性,因?yàn)镹STimer并不完全精準(zhǔn),通過這個(gè)值設(shè)置誤差范圍。

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

獲取定時(shí)器是否有效

@property (readonly, retain) id userInfo;

獲取參數(shù)信息

三、關(guān)于內(nèi)存釋放

如果我們啟動(dòng)了一個(gè)定時(shí)器,在某個(gè)界面釋放前,將這個(gè)定時(shí)器停止,甚至置為nil,都不能是這個(gè)界面釋放,原因是系統(tǒng)的循環(huán)池中還保有這個(gè)對(duì)象。所以我們需要這樣做:

-(void)dealloc{

NSLog(@"dealloc:%@",[self class]);

}

- (void)viewDidLoad {

[super viewDidLoad];

timer= [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(myLog:) userInfo:nil repeats:YES];

UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];

btn.backgroundColor=[UIColor redColor];

[btn addTarget:self action:@selector(btn) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:btn];

}

-(void)btn{

if (timer.isValid) {

[timer invalidate];

}

timer=nil;

[self dismissViewControllerAnimated:YES completion:nil];

}

在官方文檔中我們可以看到 [timer invalidate]是唯一的方法將定時(shí)器從循環(huán)池中移除。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容