2013-02-14 103 views
0

我爲我的應用程序創建了定時器,但定時器只運行一次,之後停止。NSTimer只運行一次

下面是我的代碼:

moveobjecttimer= [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(moveObject) userInfo:nil repeats:YES]; 

重複被設置好的上是,但計時器只運行一次。

我的問題是:如何創建連續循環運行而不停止的計時器?

感謝和問候

回答

0

也許你的代碼是在一個單獨的NSThread執行?在這種情況下,計時器被添加到該線程的運行循環中。如果線程被終止,在定時器第二次觸發之前,定時器只會觸發一次。

+0

我添加了額外的代碼,在runloop和主線程上添加了定時器。 – adra 2013-02-15 09:47:47

0

我添加了額外的代碼,在runloop和主線程上添加了定時器。還添加了日誌和根據日誌,定時器在所有時間都處於活動狀態。下面是代碼:NSRunLoop * runloop = [NSRunLoop currentRunLoop];self.moveobjecttimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(moveObject)userInfo:nil repeatats:YES];

[runloop addTimer:moveobjecttimer forMode:NSRunLoopCommonModes]; 
    [runloop addTimer:moveobjecttimer forMode:UITrackingRunLoopMode]; 
    [self performSelectorOnMainThread:@selector(moveobjecttimer) withObject:nil waitUntilDone:NO]; 

    BOOL timerstate = [moveobjecttimer isValid]; 
    NSLog(@"Timer validity is: %@",[email protected]"YES":@"NO"); 

動畫只顯示一次。