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?
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]; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With