-
-
Save codingwithsara/570dac457535c8dacffec7ab1219c24d to your computer and use it in GitHub Desktop.
[ iOS ] How to create a Stopwatch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ViewController.m | |
// stopwatch | |
// | |
#import "ViewController.h" | |
@interface ViewController () | |
@end | |
@implementation ViewController | |
@synthesize timerLabel,startBtn,resetBtn; | |
- (void)viewDidLoad { | |
[super viewDidLoad]; | |
running = NO; | |
count = 0; | |
timerLabel.text = @"00:00.00"; | |
startBtn.layer.cornerRadius = 45; | |
resetBtn.layer.cornerRadius = 45; | |
} | |
- (void)didReceiveMemoryWarning { | |
[super didReceiveMemoryWarning]; | |
// Dispose of any resources that can be recreated. | |
} | |
- (IBAction)startBtnPushed:(id)sender { | |
if (running == NO) { | |
running = YES; | |
[startBtn setTitle:@"STOP" forState:UIControlStateNormal]; | |
if (myTimer == nil) { | |
myTimer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(updateTimer) userInfo:nil repeats:YES]; | |
} | |
} else { | |
[self stopTimer]; | |
} | |
} | |
- (IBAction)resetBtnPushed:(id)sender { | |
[self stopTimer]; | |
count = 0; | |
timerLabel.text = @"00:00.00"; | |
} | |
- (void)stopTimer { | |
running = NO; | |
[myTimer invalidate]; | |
myTimer = nil; | |
[startBtn setTitle:@"START" forState:UIControlStateNormal]; | |
} | |
- (void)updateTimer { | |
count++; | |
int min = floor(count/100/60); | |
int sec = floor(count/100); | |
int mSec = count % 100; | |
if (sec >= 60) { | |
sec = sec % 60; | |
} | |
timerLabel.text = [NSString stringWithFormat:@"%02d:%02d.%02d",min,sec,mSec]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment