超简单的秒表:包含:开始、暂停(不清零)、清零 方法
核心代码
//
// 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、[UIColorcolorWithRed:20/255.0green:198/255.0blue:233/255.0alpha:1];
//OC 之自定义颜色
2、self.timerNSTimerscheduledTimerWithTimeInterval:0.01target:selfselector:@selector(add) userInfo:nilrepeats:YES];
//NSTimer 使用方法
3、[self.buttonStartaddTarget:selfaction:@selector(start) forControlEvents:UIControlEventTouchUpInside];
//回调方法
注意:以下为重新编辑部分:源码以及下面可以下载的源码中不包含此部分
4、 NSDateFormatterNSDateFormatteralloc]init];
setDateFormat:@"mm:ss:SS"]; //
NSDatedateWithTimeIntervalSince1970:_alltime];
stringFromDate:confromTimesp];
.label.text = confromTimespStr;
//格式化时间输出
用以上代码替换源码中的:
self.label.text = [[NSString alloc]initWithFormat:@"%02d:%02d:%02d",_alltime/6000,_alltime%6000/100,_alltime%100]; 一句
此处粗讲一下时间格式化:
把一行代码可以搞定的事情替换成一堆代码,上面的替换代码看似更麻烦了,这样做并非没有意;或许在此处显得找麻烦,但是当需要转换的时间值过大时,手动转换就会显得尤为尴尬,因为需要考虑各种因素(闰年、闰秒之类的);
系统提供的方法可以直接将数字转换成时间格式;
原理是:
dateWithTimeIntervalSince1970:_alltime]会把后面的_alltime这个数字表示的时间(s)转换成自1970年1月1日0时0分0秒0毫秒到现在的时间差(时间格式的),至于为什么是1970年的,这里不多讲;现在的计算机和一些电子设备的时间是以从当前到1970年1月1日0时0分0秒0毫秒的偏移量(ms)为标准的;这里使用dateWithTimeIntervalSince1970方法,就会把原来额double类型的数据转换成时间类型,格式由“[formatter setDateFormat:@"mm:ss:SS"]”决定;后面我会列出时间的格式化关键字;
编号 | 含义 | 备注 | 备注 |
1 | d、dd | 日(1、01) |
|
2 | EE、EEE | day(sun、sunday) |
|
3 | M、MM、MMM、MMMM | 月(1、01、Jan、January) |
|
4 | gg | 显示时代、纪元 |
|
5 | h、hh | 小时(1、01) | 12小时制 |
6 | H、HH | 小时(1、01) | 24小时制 |
7 | m、mm | 分钟(1、01) |
|
8 | s、ss | 秒(1,01) |
|
9 | S、SS、SSS | 毫秒(1位、2位、3位) |
|
10 | y、yy、yyy、yyyy | 年份 |
|
这里是个粗略的,详细可以去看看 不过其中的ff 表示并没有测试出来。