标签:
超简单的秒表:包含:开始、暂停(不清零)、清零 方法
核心代码
// // ViewController.m // MiaoBiao // // Created by Ibokan on 15/8/18. // Copyright (c) 2015年 Crazy凡. All rights reserved. // #import "ViewController.h" @interface ViewController () { int _alltime; } @property(nonatomic,strong)UIColor * EnabledColor;//定义全局可按按钮颜色 @property(nonatomic,strong)UILabel *label;//时间显示label @property(nonatomic,strong)UIButton *buttonStart;//开始按钮 @property(nonatomic,strong)UIButton *buttonPause;//暂停按钮 @property(nonatomic,strong)UIButton *buttonClear;//清零 @property(nonatomic,strong)NSTimer *timer;//计时器 @property(nonatomic,strong)UILabel *labelinfo;//作者信息 @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. _EnabledColor = [UIColor colorWithRed:20/255.0 green:198/255.0 blue:233/255.0 alpha:1]; //初始化全局颜色 //初始换显示label self.label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 320, 200)]; self.label.backgroundColor = _EnabledColor; self.label.text = @"00:00:00"; self.label.font = [UIFont fontWithName:@"Helvetica" size:50]; self.label.textColor = [UIColor whiteColor]; self.label.textAlignment = NSTextAlignmentCenter; [self.view addSubview:self.label]; //初始化开始按钮 self.buttonStart = [[UIButton alloc]initWithFrame:CGRectMake(0, 210, 155, 150)]; [self.buttonStart setTitle:@"Start" forState:UIControlStateNormal]; self.buttonStart.backgroundColor = _EnabledColor; [self.buttonStart setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; [self.view addSubview:self.buttonStart]; [self.buttonStart addTarget:self action:@selector(start) forControlEvents:UIControlEventTouchUpInside]; //初始化暂停按钮 self.buttonPause = [[UIButton alloc]initWithFrame:CGRectMake(165, 210, 155, 150)]; [self.buttonPause setTitle:@"Pause" forState:UIControlStateNormal]; self.buttonPause.backgroundColor = _EnabledColor; [self.buttonPause setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; [self.view addSubview:self.buttonPause]; [self.buttonPause addTarget:self action:@selector(pause) forControlEvents:UIControlEventTouchUpInside]; //初始化清除按钮 self.buttonClear = [[UIButton alloc]initWithFrame:CGRectMake(0, 370, 320, 150)]; [self.buttonClear setTitle:@"Clear" forState:UIControlStateNormal]; self.buttonClear.backgroundColor = _EnabledColor; [self.buttonClear setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; [self.view addSubview:self.buttonClear]; [self.buttonClear addTarget:self action:@selector(clear) forControlEvents:UIControlEventTouchUpInside]; //初始化信息显示label self.labelinfo = [[UILabel alloc]initWithFrame:CGRectMake(0, 530, 320, 40)]; self.labelinfo.backgroundColor = _EnabledColor; self.labelinfo.text = @"Developed by Crazy凡"; self.labelinfo.textColor = [UIColor whiteColor]; self.labelinfo.textAlignment = NSTextAlignmentCenter; [self.view addSubview:self.labelinfo]; [self clear];//调用clear方法为组件初始化 } - (void)start { [self.buttonStart setTitle:@"Start" forState:UIControlStateNormal];//改变开始按钮显示字符(为暂停后初始化做准备) [self startUnenabled]; [self pauseEnabled]; [self clearEnabled]; //timer 初始化 哪里需要哪里调用, self.timer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(add) userInfo:nil repeats:YES]; } - (void)pause { [self.buttonStart setTitle:@"Continue" forState:UIControlStateNormal];//改变开始按钮显示字符 [self.timer invalidate]; [self startEnabled]; [self pauseUnenabled]; } - (void)clear { _alltime = 0; // _millisecond = 0; // _second = 0; // _minute = 0; [self.timer invalidate]; self.label.text = @"00:00:00"; [self.buttonStart setTitle:@"Start" forState:UIControlStateNormal]; [self startEnabled]; [self pauseUnenabled]; [self clearUnenabled]; } - (void)add { _alltime++; self.label.text = [[NSString alloc]initWithFormat:@"%02d:%02d:%02d",_alltime/6000,_alltime%6000/100,_alltime%100]; //self.label.text = [[[NSString alloc]initWithFormat:@"%02d:%02d:%02d",_alltime/6000,_alltime%6000/100,_alltime%100] stringByAppendingString:[[NSString alloc]initWithFormat:@"%@",[[NSDate alloc]init] ]]; } //以下是按钮可用性控制方法 - (void)startEnabled { self.buttonStart.enabled = true; self.buttonStart.backgroundColor = _EnabledColor; } - (void)startUnenabled { self.buttonStart.enabled = false; self.buttonStart.backgroundColor = [UIColor darkGrayColor]; } - (void)pauseEnabled { self.buttonPause.enabled = true; self.buttonPause.backgroundColor = _EnabledColor; } - (void)pauseUnenabled { self.buttonPause.enabled = false; self.buttonPause.backgroundColor = [UIColor darkGrayColor]; } - (void)clearEnabled { self.buttonClear.enabled = true; self.buttonClear.backgroundColor = _EnabledColor; } - (void)clearUnenabled { self.buttonClear.enabled = false; self.buttonClear.backgroundColor = [UIColor darkGrayColor]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
学习点
1、[UIColor colorWithRed:20/255.0 green:198/255.0 blue:233/255.0 alpha:1];
//OC 之自定义颜色
2、self.timer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(add) userInfo:nil repeats:YES];
//NSTimer 使用方法
3、[self.buttonStart addTarget:self action:@selector(start) forControlEvents:UIControlEventTouchUpInside];
//回调方法
ioc开发学习 --简易计时器 (基于iPhone5屏幕尺寸开发)
标签:
原文地址:http://www.cnblogs.com/kongkaikai/p/4743226.html