大綱:
創(chuàng)建計(jì)時(shí)器
暫停
恢復(fù)
銷毀
開發(fā)小技巧
一、創(chuàng)建計(jì)時(shí)器
//第一個(gè)參數(shù)是間隔時(shí)間(s)
//第二個(gè)參數(shù)是self(執(zhí)行本程序的方法)
//第三個(gè)參數(shù):selector每過一個(gè)間隔時(shí)間需要執(zhí)行的方法
//第四個(gè)參數(shù):可以用來傳參
//第五個(gè)參數(shù)決定是否重復(fù)
NSTimer * timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(timer) userInfo:nil repeats:YES];
二、暫停
//暫停(可恢復(fù))
[timer setFireDate:[NSDate distantFuture]];
三、恢復(fù)
//恢復(fù)
[timer setFireDate:[NSDate distantPast]];
四、銷毀
//銷毀定時(shí)器(不可恢復(fù))
[timer invalidate];
五、開發(fā)小技巧
//在userInfo里面放入?yún)?shù)
NSTimer * timer = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(time:) userInfo:aviView repeats:NO];
//在selector的方法里面可以獲取參數(shù)并進(jìn)行操作
-(void)time:(NSTimer *)timer{
UIActivityIndicatorView * avi = [timer userInfo];
UIView * view = [self.view viewWithTag:1];
[avi stopAnimating];
[view removeFromSuperview];
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。