0

I know that this question has already been asked, I have gone through all the posts but I can't still get it to work, I guess I'm just a noob.

Here's the problem. I followed a tutorial on how to create a tap game, I'm trying to add a Pause Button so that when the player taps on it the timer(which is only for 30s) pauses, not stops, and then resume. I have tried implementing other solutions but I cannot integrate it, the program keeps crashing. Here are the methods I created:

    //Pause Game Methods
    -(void)pauseTimer {
        if(isPaused == YES) {
           //IMPLEMENT SOMETHING
        }
    }

    - (IBAction)pauseButton:(UIButton *)sender {
      isPaused = YES;
    }

    - (IBAction)resumeButton:(UIButton *)sender {
      isPaused = NO;
    }

Any help is appreciated.

Alex
  • 147
  • 12
  • This guy made a pretty good method here: http://stackoverflow.com/questions/4144242/how-to-pause-and-resume-nstimer-in-iphone – Nate Lee May 23 '14 at 13:32

2 Answers2

6

You cannot pause and resume a timer. To "pause", invalidate the timer and throw it away (you must never touch an invalidated timer). To "resume", make a new one just like the old one.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • what do you mean with throwing away? and my understanding is that if I invalidate the timer that will completely stop it, then when I create a new one it will start from the 30s not from whenever it stopped when it was invalidated. – Alex May 23 '14 at 14:32
  • Throw away means throw away (i.e. nilify your reference to it). - Timers do not "start from" anything. They just fire periodically. Any counting is up to your other code. – matt May 23 '14 at 14:34
  • @matt, in regards to invalidating the timer and creating a "new one". However, wouldn't there be a problem with this where if you pause a timer, it'll have some time left before the next timer event (i.e. you resume), but if you discard it and start a new one, it will always have the original amount of time before an event? – Pangu Mar 07 '16 at 18:30
  • @Pangu I don't know what you mean by "a problem" or "pause a timer". You cannot pause a timer. You can only stop it by invalidating it, and once you've done that, you cannot use it again. Those are just facts. – matt Mar 07 '16 at 18:36
  • @matt I apologize for the wording, from reading multiple questions regarding pausing `NSTimers` I know you cant pause it. My question is, let's say for example, you trigger an event repeatedly to run every 1 second with `scheduledTimerWithTimeInterval`, but you pause the event at 1.5 seconds, invalidate the timer. When you want to resume that event again, you create a new instance of that timer. How do you ensure that the event fires off 0.5 seconds later (remaining time) and continues firing every 1 second instead of 1 whole second later when you create a new instance of the `NSTimer`? – Pangu Mar 07 '16 at 18:49
  • @Pangu I'm having a little trouble imagining why you'd want to do that, since once you've stopped the timer, the jig is up, if you know what I mean mean. But obviously you would have to delay 0.5 seconds and create the timer to repeat every second and fire it. – matt Mar 07 '16 at 19:03
  • @matt this is the reason why I want to do that: http://stackoverflow.com/questions/35837916/why-does-my-time-uilabels-skip-2-seconds-when-it-resumes-play-from-a-pause-state :) – Pangu Mar 07 '16 at 19:16
  • 1
    @Pangu but the problem there is that you're doing it all wrong. First off, MPMoviePlayerController is deprecated. Second, you shouldn't be doing this by polling with a timer. This sort of thing is exactly what AVPlayer `addPeriodicTimeObserverForInterval:queue:usingBlock:` is for. – matt Mar 07 '16 at 19:22
0

Proper way to use & handle NSTimer is :

BOOL isTimerValid = TRUE;
myTimer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(doAction) userInfo:nil repeats:YES];

Now To stop:

if (isTimerValid == TRUE)
{
    [ScrollTimer invalidate];
    isTimerValid = FALSE;
}

Note : There is no method provided in NSTimer to pause or resume.

Mayur Prajapati
  • 5,454
  • 7
  • 41
  • 70