Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

hello I am developing small gameApp. I need to pause the timer,when user goes to another view [say settings view]. when user comes back to that view , I need to resume the timer.

can anybody solve this issue ...

Thanks in Advance...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
640 views
Welcome To Ask or Share your Answers For Others

1 Answer

NSTimer does not give you the ability to pause it. However, with a few simple variables, you can create the effect yourself:

NSTimer *timer;
double timerInterval = 10.0;
double timerElapsed = 0.0;
NSDate *timerStarted;

-(void) startTimer {
  timer = [NSTimer scheduledTimerWithTimeInterval:(timerInterval - timerElapsed) target:self selector:@selector(fired) userInfo:nil repeats:NO];
  timerStarted = [NSDate date];
}

-(void) fired {
  [timer invalidate];
  timer = nil;
  timerElapsed = 0.0;
  [self startTimer];
  // react to timer event here
}

-(void) pauseTimer {
  [timer invalidate];
  timer = nil;
  timerElapsed = [[NSDate date] timeIntervalSinceDate:timerStarted];
}

This has been working out quite well for me.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...