2

Possible Duplicate:
How to Pause/Play NSTimer?

I have three buttons start stop and pause..My start and stop button is working fine with the below code..but when press Pause it pause the timer..but when again i Press start.IT continues from the new added time ...not from the pause time....

supoose i pause at 5 second of start and wait for 5 sec then press start...it should display 5 ...but displaying 10..

because I have not mentioned (timer:) in timer!=nill of start... how it will be add..

I have problems:

  1. Pause not working.
-(void)start:(NSTimer *)timer
{
  if(_timer==nil)
  {
    startDate =[NSDate date];

    _timer=[NSTimer scheduledTimerWithTimeInterval:0.25 target:self selector:@selector(timer:) userInfo:nil repeats:YES];
  }

  if(_timer!=nil)
  { 
    float pauseTime = -1*[pauseStart timeIntervalSinceNow];

    [_timer setFireDate:[previousFireDate initWithTimeInterval:pauseTime sinceDate:previousFireDate]];
  }

}

-(void)timer:(NSTimer *)timer
{
  NSInteger secondsSinceStart = (NSInteger)[[NSDate date] timeIntervalSinceDate:startDate];

  NSInteger seconds = secondsSinceStart % 60;
  NSInteger minutes = (secondsSinceStart / 60) % 60;
  NSInteger hours = secondsSinceStart / (60 * 60);
  NSString *result = nil;
  if (hours > 0) 
  {
    result = [NSString stringWithFormat:@"%02d:%02d:%02d", hours, minutes, seconds];
  }
  else 
  {
    result = [NSString stringWithFormat:@"%02d:%02d", minutes, seconds];        
  }

  label.text=result;

  NSLog(@"time interval -> %@",result);
}

-(void)stop
{
  if(_timer!=nil)
  {
    startDate=nil;

    [_timer invalidate];
    _timer = nil; 
  }
}

-(void)pause:(NSTimer *)timer
{
  pauseStart = [NSDate dateWithTimeIntervalSinceNow:0];

  previousFireDate = [_timer fireDate];

  [_timer setFireDate:[NSDate distantFuture]];
}
Community
  • 1
  • 1
Christien
  • 1,213
  • 4
  • 18
  • 32

2 Answers2

0
-(void)pause:(NSTimer *)timer
{
  pauseStart = [NSDate dateWithTimeIntervalSinceNow:0];

  previousFireDate = [_timer fireDate];

  //[timer setFireDate:[NSDate distantFuture]];
  [_timer setFireDate:[NSDate distantFuture]];
}
9dan
  • 4,222
  • 2
  • 29
  • 44
  • ya this works...this `pause` the timer..but when I again `Start` it would not start from pausestart...its keep on counting in backside...and displays the updated time not pause time – Christien Dec 20 '12 at 08:51
  • suppose i pause at 5 second and wait for 5 second then press start ..it displays me 10 sec not 5... – Christien Dec 20 '12 at 08:52
  • this is the only problem left now – Christien Dec 20 '12 at 09:14
  • i think i got the problem...bcz when i pause...then I am starting again from the start key..and have not given (timer:) in start for pause – Christien Dec 20 '12 at 09:42
0

Here is the code that I used for a timer:

I store all the components for the time, in my (singleton) class, the hour, minute, seconds value.

And, I simply "invalidate" the timer in the "pause" method, AND I store the values.

See, if this helps.

-(void) startTimer
{
    NSLog(@"Values for timer: %d H, %d M, %d S", self.hours, self.minutes, self.seconds);

    _timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(updateTimer) userInfo:nil repeats:YES];
}

-(void) pauseTimer
{
    if(_timer)
    {
        [_timer invalidate];
    }

    _timer = nil;

    self.hour = hourValue;
    self.minute = minuteValue;
    self.second = secondsValue;
}
Ravi Raman
  • 1,070
  • 9
  • 16