nstimer在触摸屏幕时停止

问题描述:

我有一个计时器类,里面有一个nstimer,它将通知推送到一个uitableviewcontroller,并且通知对象(计时器的值)在uitableviewcell中受到影响。我的问题是当我触摸屏幕滚动,计时器停止,NSTimer不会在其他线程中启动?我如何解决这个问题。nstimer在触摸屏幕时停止

我timerClass

#import "timerEtape.h" 
#import "FonctionUtile.h" 
#import "Mission.h" 

static timerEtape *sngTimerEtape = nil; 

@implementation timerEtape 

@synthesize repeatingTimerEtape; 
@synthesize dateComp; 
@synthesize questionCircuit; 
@synthesize b_Pause; 

+(timerEtape *) singletonTimer 
{ 
    @synchronized(self){ 
     if (sngTimerEtape == nil) 
     { 
      sngTimerEtape = [[timerEtape alloc]init]; 
     } 
    } 

    return sngTimerEtape; 
} 

-(id)init 
{ 
    self = [super init]; 

    if (self != nil) { 
     dateComp = [[NSDateComponents alloc] init]; 
     b_Pause = FALSE; 
    } 

    return self; 
} 

- (void)startTimer { 

    [repeatingTimerEtape invalidate]; 

    NSString * temps; 

    if (questionCircuit) { 

     temps = [[Mission singletonMission].circuitTerrain valeurChampEtapeCircuitEnCours:@"et_temps_etape" :FALSE]; 
    } 
    else { 
     temps = [[Mission singletonMission].histoireTerrain valeurChampQuestion:@"hi_temps_etape" :FALSE]; 
    } 

    if (!b_Pause) { 
     [dateComp setHour:0]; 
     [dateComp setMinute:[[FonctionUtile gauche:temps :2] intValue]]; 
     [dateComp setSecond:[[FonctionUtile droite:temps :2] intValue]]; 
    } 
    else { 
     b_Pause = FALSE; 
    } 

    self.repeatingTimerEtape = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateLabelTimerEtape:) userInfo:nil repeats:YES]; 
} 

-(void)pause 
{ 
    self.b_Pause = TRUE; 
    [repeatingTimerEtape invalidate]; 
} 

-(void)updateLabelTimerEtape:(NSTimer*)theTimer 
{ 

    if ([dateComp second] == 0) { 

     if ([dateComp minute] == 0) { 
      if ([dateComp hour] != 0) { 
       [dateComp setHour:[dateComp hour] -1]; 
       [dateComp setMinute:59]; 
       [dateComp setSecond:59]; 
      } 
      else { 
       [repeatingTimerEtape invalidate]; 
       [delegate performSelector:touchAction]; 
      } 

     } 
     else { 
      [dateComp setMinute:[dateComp minute] -1]; 
      [dateComp setSecond:59]; 
     } 
    } 
    else { 
     [dateComp setSecond:[dateComp second] -1]; 
    } 

    [[NSNotificationCenter defaultCenter] postNotificationName:@"rafraichirTimerEtape" object:[NSString stringWithFormat:@"%02d:%02d", [dateComp minute],[dateComp second]]]; 
} 

-(void)setTarget:(id)target andAction:(SEL)action { 
    delegate = target; 
    touchAction = action; 
} 

-(void)dealloc 
{ 
    [dateComp release]; 
    [repeatingTimerEtape release]; 

    [super dealloc]; 
} 
@end 

,当我收到我的UITableViewController类

-(void)rafraichirTimerEtape:(NSNotification*)notification 
{ 
    [tempsRestant release]; 
    tempsRestant = [[notification object]copy]; 

    [table cellForRowAtIndexPath:[NSIndexPath indexPathForRow:2 inSection:0]].detailTextLabel.text = [notification object]; 
} 

感谢名单

通知我有这个问题以前也遇到过。事实上,NSTimer不会在另一个线程中运行,它总是在它启动的线程中运行。在iphone sdk中有一个runloop概念,你​​应该从here中读取它来理解定时器。在这个概念中,你将一些作业推入runloop,runloop依次实现它们。主线程始终是一个运行循环,并且ui作业在那里运行。所以如果你在主线程中启动你的定时器,它将受到UI处理的影响。您应该启动一个新线程,将其配置为运行循环并在那里启动计时器。

编辑:这是我发布的链接的示例代码。 threadMain是线程启动的第一个函数。


- (void) threadMain 
{ 
    NSRunLoop* myRunLoop = [NSRunLoop currentRunLoop]; 

    // Create and schedule the timer. 
    [NSTimer scheduledTimerWithTimeInterval:0.1 target:self 
       selector:@selector(doFireTimer:) userInfo:nil repeats:YES]; 

    NSInteger loopCount = 10; 

    do 
    { 
     // Run the run loop 10 times to let the timer fire. 
     [myRunLoop runUntilDate:[NSDate dateWithTimeIntervalSinceNow:1]]; 
     loopCount--; 
    } 
    while (loopCount); 
} 

,如果你想runloop无限运行,但有一个条件终止它,用这个来代替


BOOL shouldKeepRunning = YES;  // global 

NSRunLoop *theRL = [NSRunLoop currentRunLoop]; 

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

while (shouldKeepRunning && [theRL runMode:NSDefaultRunLoopMode 
     beforeDate:[NSDate distantFuture]]); 
+0

尼斯的答案。我可以查询一个小代码样本吗? – 2010-12-30 12:12:01