Skip to content

Instantly share code, notes, and snippets.

@codingwithsara
Created November 9, 2018 02:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codingwithsara/570dac457535c8dacffec7ab1219c24d to your computer and use it in GitHub Desktop.
Save codingwithsara/570dac457535c8dacffec7ab1219c24d to your computer and use it in GitHub Desktop.
[ iOS ] How to create a Stopwatch
//
// 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