Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I programmatically pause an NSTimer?

I'm using an NSTimer to do some rendering in an OpenGL based iPhone app. I have a modal dialog box that pops up and requests user input. While the user is providing input I'd like to "pause" i.e. something like this:

[myNSTimer pause]; 

I'm using this syntax because I've been doing things like:

[myNSTimer invalidate]; 

when I want it to stop.

How can I programmatically pause the NSTimer?

like image 702
MrDatabase Avatar asked Dec 07 '08 04:12

MrDatabase


1 Answers

Just thought of updating minor fixes to kapesoftware's answer:

NSDate *pauseStart, *previousFireDate;  -(void) pauseTimer:(NSTimer *)timer {       pauseStart = [[NSDate dateWithTimeIntervalSinceNow:0] retain];      previousFireDate = [[timer fireDate] retain];      [timer setFireDate:[NSDate distantFuture]]; }  -(void) resumeTimer:(NSTimer *)timer {      float pauseTime = -1*[pauseStart timeIntervalSinceNow];      [timer setFireDate:[previousFireDate initWithTimeInterval:pauseTime sinceDate:previousFireDate]];      [pauseStart release];      [previousFireDate release]; } 
like image 71
Thiru Avatar answered Sep 29 '22 08:09

Thiru