简单的秒表定时器 思路:1.分别添加秒表中的上部分,样式如 00 :00 : 00 . 00,分别表示时,分,秒,毫秒一共用7个labe实现2.设置按钮,分别是开始,停止,复位3,通过字符串转换成数字,和数字转换成字符串来进行秒表的设计 1 #import "Vi ...
简单的秒表定时器
思路:
1.分别添加秒表中的上部分,样式如 00 :00 : 00 . 00,分别表示时,分,秒,毫秒一共用7个labe实现
2.设置按钮,分别是开始,停止,复位
3,通过字符串转换成数字,和数字转换成字符串来进行秒表的设计
1 #import "ViewController.h" 2 3 #import "UIView+FrameExtension.h" 4 7 #define kDeviceWidth [UIScreen mainScreen].bounds.size.width 8 9 #define kDeviceHeight [UIScreen mainScreen].bounds.size.height 10 13 @interface ViewController (){ 14 15 UILabel *_lbl1; 16 17 UILabel *_lbl2; 18 19 UILabel *_lbl3; 20 21 UILabel *_lbl4; 25 NSTimer *_timer; 26 27 BOOL _isRunning; 28 29 } 33 @end 34 37 @implementation ViewController 38 41 - (void)viewDidLoad { 42 43 [super viewDidLoad]; 44 47 [self createLabel]; //创建7个标签 48 49 [self createTimer]; //创建1个定时器 50 51 [self createButton]; //创建3个按 52 53 } 54 57 -(void)createLabel{ 61 UILabel *lbl1 = [[UILabel alloc]initWithFrame:CGRectMake(90, 200, 30, 40)]; 62 63 lbl1.text = @"00"; 64 65 lbl1.textColor = [UIColor redColor]; 66 67 [self.view addSubview:lbl1]; 68 69 _lbl1 = lbl1; 70 73 UILabel *lbl11 = [[UILabel alloc]initWithFrame:CGRectMake(lbl1.right, lbl1.y, 10, lbl1.height)]; 74 75 lbl11.text = @":"; 76 77 lbl11.textColor = [UIColor redColor]; 78 79 [self.view addSubview:lbl11]; 80 83 UILabel *lbl2 = [[UILabel alloc]initWithFrame:CGRectMake(lbl11.right, lbl1.y, lbl1.width, lbl1.height)]; 84 85 lbl2.text = @"00"; 86 87 lbl2.textColor = [UIColor redColor]; 88 89 [self.view addSubview:lbl2]; 90 91 _lbl2 = lbl2; 92 95 UILabel *lbl22 = [[UILabel alloc]initWithFrame:CGRectMake(lbl2.right, lbl1.y, lbl11.width, lbl1.height)]; 96 97 lbl22.text = @":"; 98 99 lbl22.textColor = [UIColor redColor];100 101 [self.view addSubview:lbl22];102 103 UILabel *lbl3 = [[UILabel alloc]initWithFrame:CGRectMake(lbl22.right, lbl1.y, lbl1.width, lbl1.height)];104 105 lbl3.text = @"00";106 107 lbl3.textColor = [UIColor redColor];108 109 [self.view addSubview:lbl3];110 111 _lbl3 = lbl3;112 115 UILabel *lbl33 = [[UILabel alloc]initWithFrame:CGRectMake(lbl3.right, lbl1.y, lbl11.width, lbl1.height)];116 117 lbl33.text = @".";118 119 lbl33.textColor = [UIColor redColor];120 121 [self.view addSubview:lbl33]; 122 123 UILabel *lbl4 = [[UILabel alloc]initWithFrame:CGRectMake(lbl33.right, lbl1.y, lbl1.width, lbl1.height)];124 125 lbl4.text = @"00";126 127 lbl4.textColor = [UIColor redColor];128 129 [self.view addSubview:lbl4];130 131 _lbl4 = lbl4;132 137 }141 -(void)createButton{142 146 147 UIButton *btn1 = [[UIButton alloc]initWithFrame:CGRectMake(70, _lbl1.bottom+20, 60, 40)];148 149 [btn1 setTitle:@"开始" forState:UIControlStateNormal];150 151 [self.view addSubview:btn1];152 153 [btn1 setBackgroundImage:[UIImage imageNamed:@"logoff_btn_s"] forState:UIControlStateNormal];154 155 [btn1 setBackgroundImage:[UIImage imageNamed:@"submit_discussion_n"] forState:UIControlStateHighlighted];156 157 [btn1 addTarget:self action:@selector(start ) forControlEvents:UIControlEventTouchUpInside];158 163 UIButton *btn2 = [[UIButton alloc]initWithFrame:CGRectMake(btn1.right+10, btn1.y, btn1.width, btn1.height)];164 165 [btn2 setTitle:@"停止" forState:UIControlStateNormal];166 167 [self.view addSubview:btn2];168 169 [btn2 setBackgroundImage:[UIImage imageNamed:@"logoff_btn_s"] forState:UIControlStateNormal];170 171 [btn2 setBackgroundImage:[UIImage imageNamed:@"submit_discussion_n"] forState:UIControlStateHighlighted];172 173 [btn2 addTarget:self action:@selector(stop ) forControlEvents:UIControlEventTouchUpInside];174 179 UIButton *btn3 = [[UIButton alloc]initWithFrame:CGRectMake(btn2.right+10,btn1.y , btn1.width, btn1.height)];180 181 [btn3 setTitle:@"复位" forState:UIControlStateNormal];182 183 [self.view addSubview:btn3];184 185 [btn3 setBackgroundImage:[UIImage imageNamed:@"logoff_btn_s"] forState:UIControlStateNormal];186 187 [btn3 setBackgroundImage:[UIImage imageNamed:@"submit_discussion_n"] forState:UIControlStateHighlighted];188 189 [btn3 addTarget:self action:@selector(fuwei ) forControlEvents:UIControlEventTouchUpInside];190 191 }192 195 -(void)start{199 [[NSRunLoop mainRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];200 201 _isRunning = YES;202 204 205 if (_isRunning) { //表示定时器正在运行206 207 [_timer setFireDate:[NSDate distantPast]];208 211 }else{212 213 [_timer setFireDate:[NSDate distantFuture]];214 215 }216 217 }218 221 -(void)stop{222
开始
停止
复位
源文件在这里,希望可以帮到你:http://pan.baidu.com/s/1kVhHiHh
原标题:简单的秒表定时器
关键词:
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们:
admin#shaoqun.com
(#换成@)。