0

Possible Duplicate:
How can I programmatically pause an NSTimer?

I have a question. How can I pause a countdown using a timer? I am developing a game. In the game, I need to go to next view when the timer pauses, and after coming back I want to resume it.

I try this code in the view:

[mytimer pause];

// to resume
[mytimer resume];

I try that code, but I get a warning saying: "NSTimer may not respond to 'pause'"

I build with that warning and when I press the pause button, the app crashes.

Community
  • 1
  • 1
Gaurav
  • 8,227
  • 4
  • 34
  • 55
  • 1
    Search before you question: http://stackoverflow.com/questions/347219/how-can-i-programmatically-pause-an-nstimer – Kevin Sylvestre Jan 25 '11 at 06:43
  • ~ Notice I edited your question a little further than it was already. I suggest in the future you follow this previous edit example. It's not necessary to provide an opening or closing salutation, as we already know that you're thanking us by your becoming involved in the community. – jcolebrand Jan 25 '11 at 16:22
  • Ya sure,afterwards I will take care of this. – Gaurav Jan 27 '11 at 10:39

3 Answers3

8

NSTimer indeed doesn't have resume and pause methods so you can end up with a crash in runtime after such a warning. Generally you can create 2 kinds of timers (see NSTimer class reference) one that implements only once and the second, that repeats. Example:

This way you create a timer, that will enter callback myMethod each second.

NSTimer *myTimer = [NSTimer scheduledTimerWithTimeInterval:1 
    target:self 
    selector:@selector(myMethod:) 
    userInfo:nil 
    repeats:YES];

You probably will choose this one for your purpose where in your class you should maintain some

BOOL pausevariable and in the callback myMethod do the following:

 - (void) myMethod:(NSTimer *) aTimer
{
     if (!pause) {
       // do something
       // update your GUI
     }
}

where you update pause accordingly somewhere in your code. To stop the timer (and release it) call

 [myTimer invalidate];

good luck

Nava Carmon
  • 4,523
  • 3
  • 40
  • 74
1

What you want, is what OpenGLES application brings up to you. You should create 2 methods like this:

- (void)startAnimation 
{
    self.animationTimer = [NSTimer scheduledTimerWithTimeInterval:animationInterval target:self selector:@selector(selector) userInfo:nil repeats:YES];
}

- (void)stopAnimation 
{
     [animationTimer invalidate];
      animationTimer = nil;
}

It's something like this.

Oleg Danu
  • 4,149
  • 4
  • 29
  • 47
  • Once you invalidate the timer you cannot get it back unless you initialise it again.. so this code will not work if you want to come back later and run the timer. – HurkNburkS Apr 10 '13 at 22:37
  • In the context given above, isn't it the case?:) I stopAnimation by invalidating the timer and I startAnimation by initializing it again:). – Oleg Danu Apr 11 '13 at 06:17
0

Refer to the NSTimer Class Reference, there is no pause method.

WrightsCS
  • 50,551
  • 22
  • 134
  • 186