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.
Related
I am creating a game application in which I need to set timer of 2 minutes throughout the app screens in objective c.
I am creating it in viewDidLoad but it is creating a new instance every-time the view loads.
Here is the code which I am using :
#interface SomeViewController ()
{
int timerCounter;
NSTimer *timer;
}
#property (strong, nonatomic) IBOutlet UILabel *timerLbl;
#end
#implementation SomeViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self startCountdown];
}
- (void)startCountdown
{
timer = [NSTimer scheduledTimerWithTimeInterval:1
target:self
selector:#selector(countdownTimer:)
userInfo:nil
repeats:YES];
}
- (void)countdownTimer:(NSTimer *)timer
{
timerCounter--;
int minutes = timerCounter / 60;
int seconds = timerCounter % 60;
NSString *string = [NSString stringWithFormat:#"%02d", minutes];
NSString *string2 = [NSString stringWithFormat:#"%02d", seconds];
NSString *timeTotal = [string stringByAppendingString:#":"];
NSString *timeTotal2 = [timeTotal stringByAppendingString:string2];
_timerLbl.text = timeTotal2;
if (timerCounter <= 0) {
[timer invalidate];
}
}
You need to invalidate it whenever the VC deallocates
- (void)dealloc {
[timer invalidate];
}
//
The second VC may look like this
#import "ggViewController.h"
NSInteger timerCounter = 120; // declared global to hold the value
#interface ggViewController ()
{
NSTimer*timer;
}
#end
#implementation ggViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
// instead of using selector use this inline callback
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 repeats:true block:^(NSTimer * timer) {
timerCounter--;
int minutes = timerCounter / 60;
int seconds = timerCounter % 60;
NSString *string = [NSString stringWithFormat:#"%02d", minutes];
NSString *string2 = [NSString stringWithFormat:#"%02d", seconds];
NSString *timeTotal = [string stringByAppendingString:#":"];
NSString *timeTotal2 = [timeTotal stringByAppendingString:string2];
_timerLbl.text = timeTotal2;
if (timerCounter <= 0) {
[timer invalidate];
}
}];
}
-(void)viewDidDisappear:(BOOL)animated{
[super viewDidDisappear:animated];
[timer invalidate];
}
- (IBAction)gg:(id)sender {
[self dismissViewControllerAnimated:true completion:nil];
}
#end
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];
}
It is a really odd situation and I have never had a problem like this, and I would really appreciate any help that I get on this post. I have made a simple game. I will give .h and .m below.
Game30s.h :
//
// Game30s.h
// Speedy
//
// Created by Ajay Venkat on 15/10/2014.
// Copyright (c) 2014 AJTech. All rights reserved.
//
#import <UIKit/UIKit.h>
#interface Game30s : UIViewController {
IBOutlet UILabel *scorelabel;
IBOutlet UIButton *bar1;
IBOutlet UIButton *bar2;
BOOL isCracked;
BOOL isCracked2;
NSTimer *go;
NSTimer *collision;
IBOutlet UIImageView *wall;
IBOutlet UIButton *start;
IBOutlet UIButton *contin;
NSTimer *time;
NSTimer *timestart;
int timem;
IBOutlet UILabel *timemanage;
int score;
}
-(void)tensecs;
-(void)timetwo;
-(void)goRight;
-(void)col;
-(IBAction)Statt;
-(IBAction)Tap1;
-(IBAction)Tap2;
#property (nonatomic) BOOL gameCenterEnabled;
#property (nonatomic, strong) NSString *leaderboardIdentifier;
#end
Game30s.m :
//
// Game30s.m
// Speedy
//
// Created by Ajay Venkat on 15/10/2014.
// Copyright (c) 2014 AJTech. All rights reserved.
//
#import "Game30s.h"
#import GameKit;
#interface Game30s()
#end
#implementation Game30s {
int cracknum;
float speed;
int score2;
int totscore;
}
- (void)viewDidLoad {
[super viewDidLoad];
timestart = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(timetwo) userInfo:nil repeats:YES];
contin.hidden = YES;
start.hidden = NO;
bar1.hidden = YES;
bar2.hidden = YES;
[self authenticateLocalPlayer];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
-(IBAction)Tap1 {
int w = ([[UIScreen mainScreen] bounds].size.width);
int h = ([[UIScreen mainScreen] bounds].size.height);
w = w / 2;
int r1x = (arc4random() %240) + w ;
int r1y = (arc4random() %h);
bar1.center = CGPointMake(r1x, r1y);
speed = speed + 0.5;
score = score + 1;
}
-(IBAction)Tap2 {
int w = ([[UIScreen mainScreen] bounds].size.width);
int h = ([[UIScreen mainScreen] bounds].size.height);
w = w / 2;
int r2x = (arc4random() %240) + w ;
int r2y = (arc4random() %h);
speed = speed + 0.7;
bar2.center = CGPointMake(r2x, r2y);
score = score + 1;
}
-(void)goRight {
bar1.center = CGPointMake(bar1.center.x - speed, bar1.center.y);
bar2.center = CGPointMake(bar2.center.x - speed, bar2.center.y);
}
-(void)tensecs {
if (timem > 0) {
timem = timem - 1;
NSString* myString = [#(timem) stringValue];
timemanage.text = myString;
} else if (timem == 0) {
[timestart invalidate];
NSString* myString = [#(score) stringValue];
scorelabel.text = myString;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setInteger:score forKey:#"thirtysecond"];
bar1.hidden = YES;
bar2.hidden = YES;
start.hidden = NO;
start.enabled = YES;
contin.hidden = NO;
contin.enabled = YES;
[go invalidate];
[time invalidate];
}
}
-(void)col {
if (CGRectIntersectsRect(bar1.frame, wall.frame)) {
int r2x = (arc4random() %300) + 300 ;
int r2y = (arc4random() %375);
bar2.center = CGPointMake(r2x, r2y);
int r1x = (arc4random() %300) + 300 ;
int r1y = (arc4random() %375);
bar1.center = CGPointMake(r1x, r1y);
}
if (CGRectIntersectsRect(bar2.frame, wall.frame)) {
int r2x = (arc4random() %300) + 300 ;
int r2y = (arc4random() %375);
bar2.center = CGPointMake(r2x, r2y);
int r1x = (arc4random() %300) + 300 ;
int r1y = (arc4random() %375);
bar1.center = CGPointMake(r1x, r1y);
}
}
-(IBAction)Statt {
timem = 30;
score = 0;
contin.hidden = YES;
contin.enabled = NO;
time = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(tensecs) userInfo:nil repeats:YES];
bar1.hidden = NO;
bar2.hidden = NO;
int r2x = (arc4random() %300) + 300 ;
int r2y = (arc4random() %375);
bar2.center = CGPointMake(r2x, r2y);
int r1x = (arc4random() %300) + 300 ;
int r1y = (arc4random() %375);
bar1.center = CGPointMake(r1x, r1y);
cracknum = 0;
go = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:#selector(goRight) userInfo:nil repeats:YES];
collision = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:#selector(col) userInfo:nil repeats:YES];
speed = 3;
start.hidden = YES;
start.enabled = NO;
}
-(void)authenticateLocalPlayer{
// Instantiate a GKLocalPlayer object to use for authenticating a player.
GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
localPlayer.authenticateHandler = ^(UIViewController *viewController, NSError *error) {
if (viewController != nil) {
// If it's needed display the login view controller.
[self presentViewController:viewController animated:YES completion:nil];
} else {
if ([GKLocalPlayer localPlayer].authenticated) {
// If the player is already authenticated then indicate that the Game Center features can be used.
_gameCenterEnabled = YES;
// Get the default leaderboard identifier.
[[GKLocalPlayer localPlayer] loadDefaultLeaderboardIdentifierWithCompletionHandler:^(NSString *leaderboardIdentifier, NSError *error) {
if (error != nil) {
NSLog(#"%#", [error localizedDescription]);
} else{
_leaderboardIdentifier = #"thirtysecondtappers";
}
}];
} else {
_gameCenterEnabled = NO;
}
}
};
}
#end
So this is the code. In the method -(void)tensecs I have said:
NSString* myString = [#(timem) stringValue];
timemanage.text = myString;
And I linked up everything properly! Then what happens in the simulator or on any device is that the time label counts down properly from 30 down to 0. But every time a number goes down for example 29 to 28 the UIImageViews reset to their home location on the screen.
See I show screen shots here.
I tried deleting that code
NSString* myString = [#(timem) stringValue];
timemanage.text = myString;
and then it did not do this anymore. I don't know why this is happening!
Summary:
The UIImageView is going back to its home location when the timer changes a number on label! Also I have added constraints to the image on the actual designer. Is this why?
This is because of auto layout. If you haven't turned off auto layout, and move views by setting their frames, then when the view needs to redraw (like when your score label changes), the view you moved will revert to the position defined by its constraints. To fix this, you either need to reposition your view by adjusting the constraints, or turn off auto layout.
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];
}
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.