//計(jì)時(shí)器
{
//定義全局變量
//顯示時(shí)間
UILabel *_timerLabel;
//開(kāi)始計(jì)時(shí)和暫停的按鈕
UIButton*_startButton;
//復(fù)位按鈕
UIButton *_clearButton;
//計(jì)時(shí)的定時(shí)器
NSTimer *_timer;
//定義全局變量
int _second ;
int _minute ;
int _hour ;
}
- (void)viewDidLoad {
[super viewDidLoad];
_timerLabel= [[UILabel alloc] initWithFrame:CGRectMake(0, 100, 320, 50)];
_timerLabel.textAlignment=NSTextAlignmentCenter;
//枚舉類型可以用等號(hào)=數(shù)字
// _timerLabel.textAlignment = 1;
_timerLabel.font= [UIFont systemFontOfSize:30];
_timerLabel.text=@"00:00:00";
[self.view addSubview:_timerLabel];
_startButton= [[UIButton alloc] initWithFrame:CGRectMake(200, 200, 60, 60)];
_startButton.backgroundColor= [UIColor grayColor];
[_startButton setTitle:@"開(kāi)始" forState:UIControlStateNormal];
[_startButton setTitle:@"暫停" forState:UIControlStateSelected];
//圓形圖案
_startButton.layer.cornerRadius= 30;
//剪切邊界,imageview需要配置上一句使用砸捏,超出父視圖部分不顯示
// _startButton.clipsToBounds = YES;
[_startButton addTarget:self action:@selector(starBtnClick) forControlEvents:UIControlEventTouchUpInside];
[self.viewa ddSubview:_startButton];
_clearButton= [[UIButton alloc] initWithFrame:CGRectMake(60, 200, 60, 60)];
_clearButton.backgroundColor= [UIColor grayColor];
[_clearButton setTitle:@"復(fù)位" forState:UIControlStateNormal];
//圓形圖案
_clearButton.layer.cornerRadius= 30;
// _startButton.clipsToBounds = YES;
[_clearButton addTarget:self action:@selector(clearButClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_clearButton];
}
//開(kāi)始計(jì)時(shí)
-(void)starBtnClick {
//讓選中狀態(tài)取反變換按鈕的標(biāo)題
_startButton.selected = !_startButton.selected;
//判斷當(dāng)前狀態(tài)
//當(dāng)前狀態(tài)_startButton.selected == YES可以省略
// ??!_startButton.selected表示==NO
if(!_startButton.selected) {
//如果當(dāng)前不是選中狀態(tài),對(duì)應(yīng)的標(biāo)題是開(kāi)始
//關(guān)閉(銷毀)定時(shí)器
[_timer invalidate];
//銷毀定時(shí)器之后,_timer指針指向的內(nèi)容沒(méi)有對(duì)象,變成野指針,容易造成程序崩潰,所以把指針寫(xiě)為nil
//銷毀路徑防止指向錯(cuò)誤
_timer = nil;
} else {
//如果當(dāng)前是選中狀態(tài),對(duì)應(yīng)的標(biāo)題是暫停
//創(chuàng)建一個(gè)定時(shí)器控制時(shí)間的改變
_timer= [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(timerChange) userInfo:nil repeats:YES];
}
}
//清空,復(fù)位
- (void)clearButClick {
//暫停狀態(tài)中復(fù)位,顯示開(kāi)始
//把label時(shí)間歸0
_hour= 0;
_minute= 0;
_second= 0;
_timerLabel.text=@"00:00:00";
//顯示暫停,計(jì)時(shí)狀態(tài)中復(fù)位,
//判斷是否正在計(jì)時(shí),如果按鈕狀態(tài)是選中狀態(tài),表示正在計(jì)時(shí)
if(_startButton.selected) {
//銷毀定時(shí)器
[_timer invalidate];
_timer = nil;
//改變開(kāi)始按鈕狀態(tài)
_startButton.selected=NO;
}
}
//更新時(shí)間的方法
- (void)timerChange {
// ???//定義靜態(tài)變量
// ???static int second = 0;
// ???static int minute = 0;
// ???static int hour = 0;
//秒加1
_second++;
NSLog(@"%d",_second);
//如果到60秒,分鐘加1,秒歸0
if(_second == 60) {
_minute++;
_second= 0;
}
//如果到60分,時(shí)加1,分歸0
if(_minute == 60) {
_hour++;
_minute= 0;
}
//把屬性轉(zhuǎn)換為字符串
NSString *string = [NSString stringWithFormat:@"%.2d:%.2d:%.2d",_hour,_minute,_second];
//更新label的顯示內(nèi)容
_timerLabel.text= string;
}
//時(shí)鐘
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {
self.window= [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
hourLabel= [[UILabel alloc] initWithFrame:CGRectMake(10, 100, 100, 30)];
hourLabel.text=@"00";
hourLabel.backgroundColor= [UIColor yellowColor];
hourLabel.textAlignment=NSTextAlignmentCenter;
[self.window addSubview:hourLabel];
minuteLabel= [[UILabel alloc] initWithFrame:CGRectMake(110, 100, 100, 30)];
minuteLabel.text=@"00";
minuteLabel.backgroundColor= [UIColor yellowColor];
minuteLabel.textAlignment=NSTextAlignmentCenter;
[self.window addSubview:minuteLabel];
secondLabel= [[UILabel alloc] initWithFrame:CGRectMake(210, 100, 100, 30)];
secondLabel.text=@"00";
secondLabel.backgroundColor= [UIColoryellowColor];
secondLabel.textAlignment=NSTextAlignmentCenter;
[self.window addSubview:secondLabel];
timer= [NSTimer scheduledTimerWithTimeInterval:1.0 target:selfselector:@selector(timeCount) userInfo:nil repeats:YES];
// ???[timer fire];
_window.rootViewController= [[UIViewController alloc] init];
self.window.backgroundColor= [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
- (void)timeCount {
count++;
// ?/取商 。%取余
// ??130 / 60 = 2;
// ??130 % 60 = 10;
int second = count% 60;//(0 - 59)取秒
NSString *secondStr;
if(second < 10) {
secondStr = [NSString stringWithFormat:@"0%d",second];
}else{
secondStr = [NSString stringWithFormat:@"%d",second];
}
secondLabel.text= secondStr;
//假設(shè)count == 3600
int minute? = count/ 60 % 60;
NSString *minuteStr;
if(minute < 10) {
minuteStr = [NSString stringWithFormat:@"0%d",minute];
}else{
minuteStr = [NSString stringWithFormat:@"%d",minute];
}
minuteLabel.text= minuteStr;
int hour =count/ 3600 % 24;
NSString *hourStr;
if(hour < 10) {
hourStr = [NSString stringWithFormat:@"0%d",hour];
}else{
hourStr = [NSString stringWithFormat:@"%d",hour];
}
hourLabel.text= hourStr;
}