實(shí)例化計(jì)時(shí)器:
_timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(updateTimer:) userInfo:nil repeats:YES];TimeInterval:以秒為單位,多久會(huì)觸發(fā)一次;
target:一般情況下都是self;
selector:@selector 是觸發(fā)時(shí)需要執(zhí)行的事情,需要將NSTimer自身作為參數(shù)傳入;
repeats:是否重復(fù);
在上面updateTimer:(NSTimer *)sender方法中 調(diào)用sender.fireDate獲取此時(shí)觸發(fā)的時(shí)間,然后調(diào)用timeIntervalSinceDate:(NSDate *)date;也就是觸發(fā)時(shí)間和應(yīng)用開始時(shí)初始化一個(gè)date時(shí)間的差值,也就是計(jì)時(shí)器獲得的經(jīng)過多久的時(shí)間了。單位秒。
- (void)updateTimer:(NSTimer *)sender{ NSInteger deltaTime = [sender.fireDate timeIntervalSinceDate:_gameDate]; NSLog(@"%d", deltaTime); //1,2,3,4,5秒計(jì)時(shí)}
關(guān)閉計(jì)時(shí)器:調(diào)用invalidate方法停止計(jì)時(shí)器;
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注