Reputation: 62
I am new to NSTimer in iPhone. i made a stop watch. now i want to pause time in between and also want to do resume pause. my code is given bellow.
Start Time:
startDate = [[NSDate date]retain];
stopWatchTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/10.0
target:self
selector:@selector(updateTimer)
userInfo:nil
repeats:YES];
Stop Time:
[stopWatchTimer invalidate];
stopWatchTimer = nil;
[self updateTimer];
Update Time:
(void)updateTimer
{
NSDate *currentDate = [NSDate date];
NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:startDate];
NSDate *timerDate = [NSDate dateWithTimeIntervalSince1970:timeInterval];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"mm:ss"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]];
NSString *timeString=[dateFormatter stringFromDate:timerDate];
stopWatchLabel.text = timeString;
[dateFormatter release];
}
Upvotes: 0
Views: 5259
Reputation: 3080
I created some extra variables to help keep track on the elapsed time:
BOOL watchStart;
NSTimeInterval pauseTimeInterval;
I set watchStart = NO and pauseTimeInterval = 0.0.
I used watchStart to check if its a start or pause condition (for my IBAction
), and in updateTimer
, I set the timeInterval
to pauseTimeInterval
.
The crucial part of the code is:
_startDate = [_startDate dateByAddingTimeInterval:((-1)*(_pauseTimeInterval))]
That line will add the extra timeInterval
to the startDate
for calculation of the time that was paused. It's kind of like "stepping back" in time to add in the interval that was previously recorded.
-(void) updateTimer {
NSDate *currentDate = [NSDate date];
NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:_startDate];
NSDate *timerDate = [NSDate dateWithTimeIntervalSince1970:timeInterval];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"mm:ss.S"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]];
NSString *timeString = [dateFormatter stringFromDate:timerDate];
_stopWatchLabel.text = timeString;
_pauseTimeInterval = timeInterval;
}
-(IBAction)btnStartPause:(id)sender {
if(_watchStart == NO) {
_watchStart = YES;
_startDate = [NSDate date];
_startDate = [_startDate dateByAddingTimeInterval:((-1)*(_pauseTimeInterval))];
_stopWatchTime = [NSTimer scheduledTimerWithTimeInterval:1.0/10.0 target:self selector:@selector(updateTimer) userInfo:nil repeats:YES];
}
else {
_watchStart = NO;
[_stopWatchTime invalidate];
_stopWatchTime = nil;
[self updateTimer];
}
Upvotes: 3
Reputation: 1
Friends i was successful in implementing pause and resume function to iOs timer app, the code is as follows
- (IBAction)pauseT:(id)sender {
se = sec;
mi = min;
ho = hour;
[timer invalidate];
}
- (IBAction)resumeT:(id)sender {
sec = se;
min = mi;
hour = ho;
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(countDown) userInfo:nil repeats:YES];
}
Hope this works for you guys..
Upvotes: 0
Reputation:
-(void)startStopwatch
{
//initialize the timer HUD
[self setSeconds:second];
// [self.hud.stopwatch setSeconds:_secondsLeft];
//schedule a new timer
_timer = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:@selector(tick:)
userInfo:nil
repeats:YES];
}
-(void)pause{
[_timer invalidate];
}
-(void)resume {
[_timer isValid];
}
using this code we can pause and resume the timer
Upvotes: 0
Reputation: 21097
You can use NSTimeInterval instead of timer. I have a functional code to pause and stop the timer.
@interface PerformBenchmarksViewController () {
int currMinute;
int currSecond;
int currHour;
int mins;
NSDate *startDate;
NSTimeInterval secondsAlreadyRun;
}
@end
- (void)viewDidLoad
{
[super viewDidLoad];
running = false;
}
- (IBAction)StartTimer:(id)sender {
if(running == false) {
//start timer
running = true;
startDate = [[NSDate alloc] init];
startTime = [NSDate timeIntervalSinceReferenceDate];
[sender setTitle:@"Pause" forState:UIControlStateNormal];
[self updateTime];
}
else {
//pause timer
secondsAlreadyRun += [[NSDate date] timeIntervalSinceDate:startDate];
startDate = [[NSDate alloc] init];
[sender setTitle:@"Start" forState:UIControlStateNormal];
running = false;
}
}
- (void)updateTime {
if(running == false) return;
//calculate elapsed time
NSTimeInterval currentTime = [NSDate timeIntervalSinceReferenceDate];
NSTimeInterval elapsed = secondsAlreadyRun + currentTime - startTime;
// extract out the minutes, seconds, and hours of seconds from elapsed time:
int hours = (int)(mins / 60.0);
elapsed -= hours * 60;
mins = (int)(elapsed / 60.0);
elapsed -= mins * 60;
int secs = (int) (elapsed);
//update our lable using the format of 00:00:00
timerLabel.text = [NSString stringWithFormat:@"%02u:%02u:%02u", hours, mins, secs];
//call uptadeTime again after 1 second
[self performSelector:@selector(updateTime) withObject:self afterDelay:1];
}
Hope this will help. Thanks
Upvotes: 5
Reputation: 1
Small correction for the above example:
_startDate = [_startDate dateByAddingTimeInterval:((-1)*(_pauseTimeInterval))];
Add retain @ the end :
_startDate = [[_startDate dateByAddingTimeInterval:((-1)*(_pauseTimeInterval))]retain];
This will make things better :-)
Upvotes: 0
Reputation: 3045
NSTimer
does not have pause or resume methods. You can make 2 types of timers, one that implements only once and the second, that repeats. Example:
Creates 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 in your code. To stop the timer and release memory, call
[myTimer invalidate];
hope this helped.
Upvotes: 1