How to make a persistent (multitasking-aware) stopwatch? - ios

I am using NSTimer to make a stopwatch. I would like it to continue working if the user switches to a different app, but right now it only works when the app is running. I figure that this is probably pretty simple to fix by recording a timestamp when the timer starts, but I'm not really sure how to reconcile that with the ability to pause/restart the timer in the middle.
My code:
-(IBAction)start;
{
if(sharedInstance.timer == nil){
sharedInstance.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(showActivity) userInfo:nil repeats:YES];
sharedInstance.timerRunning = YES;
}
}
-(IBAction)stop;
{
[sharedInstance.timer invalidate];
sharedInstance.timer = nil;
sharedInstance.timerRunning = NO;
}
-(void)showActivity;
{
sharedInstance.elapsedTime += 1;
int hours = sharedInstance.elapsedTime / 3600 ;
int minutes = sharedInstance.elapsedTime / 60 - hours * 60;
int seconds = sharedInstance.elapsedTime - hours * 3600 - minutes * 60;
sharedInstance.timeString = [NSString stringWithFormat:#"%02d:%02d:%02d", hours, minutes, seconds];
self.readout.text = sharedInstance.timeString;
}
-(void)viewDidLoad {
sharedInstance = [DataSingleton sharedInstance];
if (sharedInstance.timeString==nil) {
sharedInstance.timeString=#"00:00:00";
}
self.readout.text = sharedInstance.timeString;
[sharedInstance.timer invalidate];
sharedInstance.timer = nil;
if(sharedInstance.timerRunning) {
[self start];
}
[super viewDidLoad];
}
-(void)reset {
[self stop];
sharedInstance.elapsedTime = 0;
sharedInstance.timeString = #"00:00:00";
self.readout.text = sharedInstance.timeString;
}

Every time you call showActivity you could get a new time stamp and subtract the old one from it getting the change in time. Then add that to elapsedTime instead of 1.

Related

How to set progress bar With timer after view controller loaded

I m using timer on circular progress bar and taken NSDate property in interface (tested also taking in viewWillAppear) but when viewController loads it show starting from 27 seconds remain, while i have set its value to 30 seconds.
My scenario is, a user has a question and optional answers and he/she has some time suppose 30 seconds when he/she click start button than another screen opens after this screen loads time start should be from 30 seconds.
#interface TestViewController ()
{
NSTimeInterval totalCountdownInterval;
NSDate* startDate;
}
}
-(void)viewWillAppear:(BOOL)animated{
[self startTimer];
totalCountdownInterval = 30.0;
startDate = [NSDate date];
}
-(void)startTimer {
if (!_timer) {
_timer = [NSTimer scheduledTimerWithTimeInterval:1.0f
target:self
selector:#selector(timerFired:)
userInfo:nil
repeats:YES];
}
}
-(void)stopTimer {
if ([_timer isValid]) {
[_timer invalidate];
}
_timer = nil;
}
-(void)timerFired:(NSTimer *)timer {
NSTimeInterval elapsedTime = [[NSDate date] timeIntervalSinceDate:startDate];
NSTimeInterval remainingTime = totalCountdownInterval - elapsedTime;
CGFloat timeRemain = remainingTime;
NSLog(#"%f", timeRemain);
if (remainingTime < 0.0) {
[_timer invalidate];
}
[_circleProgressBar setHintTextGenerationBlock:(^NSString *(CGFloat progress) {
if (!(timeRemain < 0.9)) {
progress = timeRemain - 1;
return [NSString stringWithFormat:#"%.0f", progress];
}else{
progress = 0;
return [NSString stringWithFormat:#"%.0f", progress];
}
})];
[_circleProgressBar setProgress:(_circleProgressBar.progress + 0.0333f) animated:YES];
}
-(void) viewWillDisappear:(BOOL)animated {
[self stopTimer];
}
Any help will be appreciated. Thanks.
The first timer event will be fired after 1 second is passed, so you should see 29 first. So you should display the number 30 manually somehow, outside the timer event.
And you subtract 1 in your code in setHintTextGenerationBlock :
progress = timeRemain - 1;
which will make you see 28 as initial.
And as final you should start timer in viewDidAppear instead of viewWillAppear where you lose another second and come to 27.
To correct code should be like:
#interface TestViewController ()
{
NSTimeInterval totalCountdownInterval;
NSDate* startDate;
}
}
-(void)viewDidLoad {
[_circleProgressBar setHintTextGenerationBlock:(^NSString *(CGFloat progress) {
return [NSString stringWithFormat:#"%.0f", progress * 30];
})];
[_circleProgressBar setProgress:1.0) animated:NO];
}
-(void)viewDidAppear:(BOOL)animated{
[self startTimer];
totalCountdownInterval = 30.0;
startDate = [NSDate date];
}
-(void)startTimer {
if (!_timer) {
_timer = [NSTimer scheduledTimerWithTimeInterval:1.0f
target:self
selector:#selector(timerFired:)
userInfo:nil
repeats:YES];
}
}
-(void)stopTimer {
if ([_timer isValid]) {
[_timer invalidate];
}
_timer = nil;
}
-(void)timerFired:(NSTimer *)timer {
NSTimeInterval elapsedTime = [[NSDate date] timeIntervalSinceDate:startDate];
NSTimeInterval remainingTime = totalCountdownInterval - elapsedTime;
CGFloat timeRemain = remainingTime;
NSLog(#"%f", timeRemain);
if (remainingTime < 0.0) {
[_timer invalidate];
}
[_circleProgressBar setProgress:(1.0 * timeRemain / 30.0) animated:YES];
}
-(void) viewWillDisappear:(BOOL)animated {
[self stopTimer];
}
You should set progress to 1.0 in the beginning and count down to 0.0 in 30 steps.
Set hint generation block to display something meaningful for 1.0 - 0.0 as 30 - 0.
use that timer code in void view did appear and try once.
- (void)viewDidAppear:(BOOL)animated
{
[self startTimer];
totalCountdownInterval = 30.0;
startDate = [NSDate date];
}

How to add milliseconds to a stopwatch app?

Ok, so I've based my stopwatch app code from this tutorial right here http://iphonedev.tv/blog/2013/7/7/getting-started-part-3-adding-a-stopwatch-with-nstimer-and-our-first-class
I like the way it is set up, but I can't figure out how to add hundredths of a second to it, anyone know how to do this?
My ViewController.m file
#import "ViewController.h"
#import "Foundation/Foundation.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (NSTimer *)createTimer
{
return [NSTimer scheduledTimerWithTimeInterval:0.01
target:self
selector:#selector(timerTicked:)
userInfo:nil
repeats:YES];
}
- (void)timerTicked:(NSTimer *)timer
{
_currentTimeInSeconds++;
self.timeLabel.text = [self formattedTime:_currentTimeInSeconds];
}
- (NSString *)formattedTime:(int)totalSeconds
{
int hundredths = totalSeconds % 60;
int seconds = totalSeconds % 60;
int minutes = (totalSeconds / 60) % 60;
int hours = totalSeconds / 3600;
return [NSString stringWithFormat:#"%02d:%02d:%02d.%02d", hours, minutes, seconds, hundredths];
}
- (IBAction)startButtonPressed:(id)sender
{
if (!_currentTimeInSeconds)
{
_currentTimeInSeconds = 0 ;
}
if (!_theTimer)
{
_theTimer = [self createTimer];
}
}
- (IBAction)stopButtonPressed:(id)sender
{
[_theTimer invalidate];
}
- (IBAction)resetButtonPressed:(id)sender
{
if (_theTimer)
{
[_theTimer invalidate];
_theTimer = [self createTimer];
}
_currentTimeInSeconds = 0;
self.timeLabel.text = [self formattedTime:_currentTimeInSeconds];
}
#end
Thanks again for anybody who can help!
First, you should change the name of your variable from _currentTimeInSeconds to _currentTimeInHundredths (or something shorter if you want).
Next, you need to update the logic in your - (NSString *)formattedTime:(int)totalSeconds method. Try something like this (changing totalSeconds to totalHundredths for the same reason as before).
int hours = totalHundredths / 360000;
int minutes = (totalHundredths - (hours * 360000)) / 6000;
int seconds = (totalHundredths - (hours * 360000) - (minutes * 6000)) / 100;
int hundredths = totalHundredths - (hours * 360000) - (minutes * 6000) - (seconds * 100);
I haven't tested the math on the numbers, but they should be right.

Counting time spent in background for an app using NSNotificationCenter

I am making a stopwatch, but it stops counting when the app is put into the background. I have tried to count the time that the app spends in the background, and then use NSNotificationCenter to send that time in seconds to my StopwatchViewController where I can add on the elapsed time. However, it does not seem to work:
In my AppDelegate.m file:
- (void)applicationDidEnterBackground:(UIApplication *)application
{
NSDate *currentDate= [NSDate date];
[[NSUserDefaults standardUserDefaults] setObject:currentDate forKey:#"backgroundDate"];
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
NSDate *dateWhenAppGoesBg= (NSDate *)[[NSUserDefaults standardUserDefaults] objectForKey:#"backgroundDate"];
NSTimeInterval timeSpentInBackground = [[NSDate date] timeIntervalSinceDate:dateWhenAppGoesBg];
NSNumber *n = [NSNumber numberWithDouble:timeSpentInBackground];
[[NSNotificationCenter defaultCenter] postNotificationName:#"NEWMESSAGE" object:n];
NSLog(#"%d", [n integerValue]);
}
In my StopwatchViewController.m file:
- (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)nibBundle { // Initialise view controller
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(newMessageReceived:) name:#"NEWMESSAGE" object:nil];
return self;
}
-(void)newMessageReceived:(NSNotification *) notification{
elapsedTime = [[notification object] intValue];
elapsedHours = elapsedTime / 3600;
elapsedTime = elapsedTime - (elapsedTime % 3600);
elapsedMinutes = elapsedTime / 60;
elapsedTime = elapsedTime - (elapsedTime % 60);
elapsedSeconds = elapsedTime;
secondInt = secondInt + elapsedSeconds;
if (secondInt > 59) {
++minuteInt;
secondInt -= 60;
}
minuteInt = minuteInt + elapsedMinutes;
if (minuteInt > 59) {
++hourInt;
minuteInt -= 60;
}
hourInt = hourInt + elapsedHours;
if (hourInt > 23) {
hourInt = 0;
}
}
If I am not completely missing the point, I think you are attacking the problem in the wrong way.
If you are creating a stopwatch, the only two interesting points in time are the point when you started the stopwatch and the current time. There is no reason to calculate the time that passed when your app was in the background.
Instead, just store the point in time where your stopwatch was started, then add e.g. a NSTimer that updates the timer display by comparing this time with the current time (i.e. [NSDate date). Then you won't have to worry about what happens when your app enter background mode.
EDIT Some ideas (disclaimer: did not have access to Xcode, so I just typed this up from my head):
When the user starts the timer, save the current time and start a NSTimer
- (void) didTapStart:(id)sender {
self->startTime = [NSDate date];
[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:#selector(timerElapsed:) userInfo:nil repeats:YES];
}
Then update the display on the timer events
- (void) timerElapsed:(id)sender {
NSDateInterval elapsed = [[NSDate date] timeIntervalSinceDate:self->startTime];
int hours = (int)elapsed / 3600;
int minutes = ((int)elapsed / 60) % 60;
int seconds = (int)elapsed % 60;
NSString* elapsedString = [NSString stringWithFormat:#"Elapsed: %d:%02d:%02d",hours,minutes,seconds];
}

How to set nstimer in reverse order

I have 3 UIlabel
First UILabel show- hh(hours)
Second UILabel show- mm(minutes)
Third UILabel show- ss(seconds)
Time is already showing in UILabels , but it has to run or tick reversely.
Means- 10:04:45 will come to 00:00:00--
This is what i am trying.
#pragma mark - Timer Function
-(NSArray *)getTimeFromSring:(NSString *)time
{
//only accepting time format hh:mm:ss
NSMutableArray *resultArr=[[NSMutableArray alloc]init];
int count=1;
for(int i=0;i<[time length];i++)
{
unichar ch = [time characterAtIndex: i];
if(ch!=':' && count<=3)
{
NSString *appendString = [NSString stringWithFormat:#"%c",ch];
[resultArr addObject:appendString];
}
else
{
count++;
}
}
return resultArr;
}
Finally
-(void)start:(NSTimer *)timer
{
if(_timer==nil)
{
startDate =[NSDate date];
_timer=[NSTimer scheduledTimerWithTimeInterval:0.25 target:self selector:#selector(timer:) userInfo:nil repeats:YES];
}
if(_timer!=nil)
{
float pauseTime = -1*[pauseStart timeIntervalSinceNow];
[_timer setFireDate:[previousFireDate initWithTimeInterval:pauseTime sinceDate:previousFireDate]];
}
}
-(void)timer:(NSTimer *)timer
{
NSInteger secondsSinceStart = (NSInteger)[[NSDate date] timeIntervalSinceDate:startDate];
NSInteger seconds = secondsSinceStart % 60;
NSInteger minutes = (secondsSinceStart / 60) % 60;
NSInteger hours = secondsSinceStart / (60 * 60);
NSString *result = nil;
if (hours > 0)
{
result = [NSString stringWithFormat:#"%02d:%02d:%02d", hours, minutes, seconds];
}
else
{
result = [NSString stringWithFormat:#"%02d:%02d", minutes, seconds];
}
label.text=result;
NSLog(#"time interval -> %#",result);
}
-(void)stop
{
if(_timer!=nil)
{
endDate = [NSDate date];
NSLog(#"endate%#",endDate);
NSTimeInterval interval = [endDate timeIntervalSinceDate:startDate];
NSLog(#"total time %f",interval);
[_timer invalidate];
_timer = nil;
startDate=nil;
}
}
A couple of possible approaches, depending on the specifics of what you want to do:
Keep track of the elapsed time and subtract it from the start time.
Calculate the end time at the start and figure out the difference between the current time and then.
Take a look at these examples, maybe they will help you:)
https://github.com/mineschan/MZTimerLabel
https://github.com/TriggerTrap/TTCounterLabel

NSTimer Pause timer [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to Pause/Play NSTimer?
I have three buttons start stop and pause..My start and stop button is working fine with the below code..but when press Pause it pause the timer..but when again i Press start.IT continues from the new added time ...not from the pause time....
supoose i pause at 5 second of start and wait for 5 sec then press start...it should display 5 ...but displaying 10..
because I have not mentioned (timer:) in timer!=nill of start...
how it will be add..
I have problems:
Pause not working.
-(void)start:(NSTimer *)timer
{
if(_timer==nil)
{
startDate =[NSDate date];
_timer=[NSTimer scheduledTimerWithTimeInterval:0.25 target:self selector:#selector(timer:) userInfo:nil repeats:YES];
}
if(_timer!=nil)
{
float pauseTime = -1*[pauseStart timeIntervalSinceNow];
[_timer setFireDate:[previousFireDate initWithTimeInterval:pauseTime sinceDate:previousFireDate]];
}
}
-(void)timer:(NSTimer *)timer
{
NSInteger secondsSinceStart = (NSInteger)[[NSDate date] timeIntervalSinceDate:startDate];
NSInteger seconds = secondsSinceStart % 60;
NSInteger minutes = (secondsSinceStart / 60) % 60;
NSInteger hours = secondsSinceStart / (60 * 60);
NSString *result = nil;
if (hours > 0)
{
result = [NSString stringWithFormat:#"%02d:%02d:%02d", hours, minutes, seconds];
}
else
{
result = [NSString stringWithFormat:#"%02d:%02d", minutes, seconds];
}
label.text=result;
NSLog(#"time interval -> %#",result);
}
-(void)stop
{
if(_timer!=nil)
{
startDate=nil;
[_timer invalidate];
_timer = nil;
}
}
-(void)pause:(NSTimer *)timer
{
pauseStart = [NSDate dateWithTimeIntervalSinceNow:0];
previousFireDate = [_timer fireDate];
[_timer setFireDate:[NSDate distantFuture]];
}
-(void)pause:(NSTimer *)timer
{
pauseStart = [NSDate dateWithTimeIntervalSinceNow:0];
previousFireDate = [_timer fireDate];
//[timer setFireDate:[NSDate distantFuture]];
[_timer setFireDate:[NSDate distantFuture]];
}
Here is the code that I used for a timer:
I store all the components for the time, in my (singleton) class, the hour, minute, seconds value.
And, I simply "invalidate" the timer in the "pause" method, AND I store the values.
See, if this helps.
-(void) startTimer
{
NSLog(#"Values for timer: %d H, %d M, %d S", self.hours, self.minutes, self.seconds);
_timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:#selector(updateTimer) userInfo:nil repeats:YES];
}
-(void) pauseTimer
{
if(_timer)
{
[_timer invalidate];
}
_timer = nil;
self.hour = hourValue;
self.minute = minuteValue;
self.second = secondsValue;
}

Resources