티스토리 뷰

* 해당글은 GitHub 에 올린 것을 옮긴것이기 때문에 별도의 설명이 없습니다. 

* 전체를 보고 싶으신 분들은 아래 링크를 참고해주시길 바랍니다.

https://github.com/larooly/StopWatch_iOS

 

GitHub - larooly/StopWatch_iOS: iOS simple Stopwatch

iOS simple Stopwatch. Contribute to larooly/StopWatch_iOS development by creating an account on GitHub.

github.com

완성본

ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
{
    NSTimer *timer;
    BOOL runnig;
    int count;
}
@property (weak, nonatomic) IBOutlet UILabel *timerLabel;
@property (weak, nonatomic) IBOutlet UIButton *resetBtn;
@property (weak, nonatomic) IBOutlet UIButton *startBtn;

- (IBAction)startBtnPushed:(id)sender;
- (IBAction)resetBtnPushed:(id)sender;

-(void)updateTimer;

@end

ViewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize timerLabel,startBtn,resetBtn;
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    runnig =NO;
    count=0;
    timerLabel.text=@"00:00.00";
    startBtn.layer.cornerRadius=45;
    resetBtn.layer.cornerRadius=45;
    
}


- (IBAction)resetBtnPushed:(id)sender {
    [self stoptimer];
    count=0;
    timerLabel.text = @"00:00.00";
}

- (IBAction)startBtnPushed:(id)sender {
    if(runnig==NO){
        runnig=YES;
        [startBtn setTitle:@"STOP" forState:UIControlStateNormal];
        if(timer ==nil){
            timer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(updateTimer) userInfo:nil repeats:YES];
        }
    }else{
        [self stoptimer];
       
    }
}
-(void)stoptimer{
    runnig=NO;
    [timer invalidate];
    timer = 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
댓글