5

I have a game which uses a timer. I want to make it so the user can select a button and it pauses that timer and when they click that button again, it will unpause that timer. I already have code to the timer, just need some help with the pausing the timer and the dual-action button.

Code to timer:

-(void)timerDelay {

    mainInt = 36;

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

-(void)countDownDuration {

    MainInt -= 1;

    seconds.text = [NSString stringWithFormat:@"%i", MainInt];
    if (MainInt <= 0) {
        [timer invalidate];
        [self delay];
    }

}
madth3
  • 7,275
  • 12
  • 50
  • 74
Kyle Greenlaw
  • 737
  • 1
  • 8
  • 20
  • Marking TBlue's answer as correct was correct if it helped you. You don't have to edit that code into your question or add [SOLVED] in the title. I'd recommend reading the [help] to learn more about the proper ways of posting in StackOverflow. – madth3 Aug 14 '13 at 00:22

2 Answers2

18

That's very easy.

// Declare the following variables
BOOL ispaused;
NSTimer *timer;
int MainInt;

-(void)countUp {
    if (ispaused == NO) {
        MainInt +=1;
        secondField.stringValue = [NSString stringWithFormat:@"%i",MainInt];
    }
}

- (IBAction)start1Clicked:(id)sender {
    MainInt=0;
    timer=[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(countUp) userInfo:Nil repeats:YES];
}

- (IBAction)pause1Clicked:(id)sender {
    ispaused = YES;
}

- (IBAction)resume1Clicked:(id)sender {
    ispaused = NO;
}
El Tomato
  • 6,479
  • 6
  • 46
  • 75
4

There is no pause and resume functionality in NSTimer. You can impliment it like below code.

- (void)startTimer
{
    m_pTimerObject = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self  selector:@selector(fireTimer:) userInfo:nil repeats:YES];
}

- (void)fireTimer:(NSTimer *)inTimer
{
    // Timer is fired.
}

- (void)resumeTimer
{
    if(m_pTimerObject)
    {
        [m_pTimerObject invalidate];
        m_pTimerObject = nil;        
    }
    m_pTimerObject = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self  selector:@selector(fireTimer:) userInfo:nil repeats:YES];
}

- (void)pauseTimer
{
    [m_pTimerObject invalidate];
    m_pTimerObject = nil;
}
dreamlax
  • 93,976
  • 29
  • 161
  • 209
Adrian P
  • 6,479
  • 4
  • 38
  • 55
  • 6
    This doesn't pause, it just stops and restarts. – Carl Veazey Aug 13 '13 at 03:53
  • @CarlVeazey As the answer mentions, there is no "pause" functionality in `NSTimer`, so any answer (including the accepted one) must work around that fact. This answer is better than the accepted one because it does not keep a timer which is doing nothing valuable running. +1 – Nate Chandler Aug 17 '13 at 15:29
  • @Nate the answers in the duplicate seem to work around this fact while still offering pause functionality. I don't think it's unreasonable to point that out. – Carl Veazey Aug 17 '13 at 18:32
  • @Nate Chandler, thanks for pointing that out. What I don't understand is the vote down on an answer which is clearly correct. There should be an explenation required when someone votes down an answer. – Adrian P Aug 17 '13 at 19:36