//计时器
{
//定义全局变量
//显示时间
UILabel *_timerLabel;
//开始计时和暂停的按钮
UIButton*_startButton;
//复位按钮
UIButton *_clearButton;
//计时的定时器
NSTimer *_timer;
//定义全局变量
int _second ;
int _minute ;
int _hour ;
}
- (void)viewDidLoad {
[super viewDidLoad];
_timerLabel= [[UILabel alloc] initWithFrame:CGRectMake(0, 100, 320, 50)];
_timerLabel.textAlignment=NSTextAlignmentCenter;
//枚举类型可以用等号=数字
// _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:@"开始" 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:@"复位" forState:UIControlStateNormal];
//圆形图案
_clearButton.layer.cornerRadius= 30;
// _startButton.clipsToBounds = YES;
[_clearButton addTarget:self action:@selector(clearButClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_clearButton];
}
//开始计时
-(void)starBtnClick {
//让选中状态取反变换按钮的标题
_startButton.selected = !_startButton.selected;
//判断当前状态
//当前状态_startButton.selected == YES可以省略
// !_startButton.selected表示==NO
if(!_startButton.selected) {
//如果当前不是选中状态,对应的标题是开始
//关闭(销毁)定时器
[_timer invalidate];
//销毁定时器之后,_timer指针指向的内容没有对象,变成野指针,容易造成程序崩溃,所以把指针写为nil
//销毁路径防止指向错误
_timer = nil;
} else {
//如果当前是选中状态,对应的标题是暂停
//创建一个定时器控制时间的改变
_timer= [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(timerChange) userInfo:nil repeats:YES];
}
}
//清空,复位
- (void)clearButClick {
//暂停状态中复位,显示开始
//把label时间归0
_hour= 0;
_minute= 0;
_second= 0;
_timerLabel.text=@"00:00:00";
//显示暂停,计时状态中复位,
//判断是否正在计时,如果按钮状态是选中状态,表示正在计时
if(_startButton.selected) {
//销毁定时器
[_timer invalidate];
_timer = nil;
//改变开始按钮状态
_startButton.selected=NO;
}
}
//更新时间的方法
- (void)timerChange {
// //定义静态变量
// 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分,时加1,分归0
if(_minute == 60) {
_hour++;
_minute= 0;
}
//把属性转换为字符串
NSString *string = [NSString stringWithFormat:@"%.2d:%.2d:%.2d",_hour,_minute,_second];
//更新label的显示内容
_timerLabel.text= string;
}
//时钟
- (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;
//假设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;
}