简单的秒表定时器
思路:
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
225 _isRunning = !_isRunning; 226 229 if (_isRunning) { // 表示定时器正在运行 230 233 }else{ 234 235 [_timer setFireDate:[NSDate distantFuture]]; 236 237 } 238 239 _isRunning = NO; 240 241 } 242 245 -(void)fuwei{ 246 251 NSString* str4 = _lbl4.text; 252 253 NSString* str3 = _lbl3.text; 254 255 NSString* str2 = _lbl2.text; 256 257 NSString* str1 = _lbl1.text; 258 261 int x4 = [str4 intValue]; 262 263 int x3 = [str3 intValue]; 264 265 int x2 = [str2 intValue]; 266 267 int x1 = [str1 intValue]; 268 271 x4 = x3 = x2 = x1 = 0; 272 275 NSString* str44 = [NSString stringWithFormat:@"%02d",x4]; 276 277 NSString* str33 = [NSString stringWithFormat:@"%02d",x3]; 278 279 NSString* str22 = [NSString stringWithFormat:@"%02d",x2]; 280 281 NSString* str11 = [NSString stringWithFormat:@"%02d",x1]; 282 283 _lbl4.text = str44; 284 285 _lbl3.text = str33; 286 287 _lbl2.text = str22; 288 289 _lbl1.text = str11; 290 293 _isRunning = !_isRunning; 294 297 if (_isRunning) { // 表示定时器正在运行 298 301 }else{ 302 303 [_timer setFireDate:[NSDate distantFuture]]; 304 305 } 306 307 _isRunning = NO; 308 311 } 312 315 -(void)createTimer{ 319 _timer = [NSTimer timerWithTimeInterval:0.01 target:self selector:@selector(move ) userInfo:nil repeats:YES]; 320 323 _isRunning = NO; 324 327 // 下面这种方法创建的定时器,会自动的加入运行循环 328 329 // _timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(move) userInfo:nil repeats:YES]; 330 331 } 332 334 335 -(void)move{ 336 339 NSString* str4 = _lbl4.text; 340 341 NSString* str3 = _lbl3.text; 342 343 NSString* str2 = _lbl2.text; 344 345 NSString* str1 = _lbl1.text; 346 349 int x4 = [str4 intValue]; 350 351 int x3 = [str3 intValue]; 352 353 int x2 = [str2 intValue]; 354 355 int x1 = [str1 intValue]; 356 359 x4++; 360 363 if (x4 == 100 ) { 364 365 x3 = x3 + 1; 366 367 x4 = 0; 368 369 if (x3 == 60) { 370 371 x2 = x2 + 1; 372 373 x3 = 0; 374 375 if (x2 == 60 ) { 376 377 x1 = x1 + 1; 378 379 x2 = 0; 380 381 if (x1 == 24 ) { 382 383 x4 = x3 = x2 = x1 = 0; 384 385 } 386 387 } 388 389 } 390 391 } 392 394 395 NSString* str44 = [NSString stringWithFormat:@"%02d",x4]; 396 397 NSString* str33 = [NSString stringWithFormat:@"%02d",x3]; 398 399 NSString* str22 = [NSString stringWithFormat:@"%02d",x2]; 400 401 NSString* str11 = [NSString stringWithFormat:@"%02d",x1]; 402 403 _lbl4.text = str44; 404 405 _lbl3.text = str33; 406 407 _lbl2.text = str22; 408 409 _lbl1.text = str11; 410 411 } 412 413 @end
开始
停止
复位
源文件在这里,希望可以帮到你:http://pan.baidu.com/s/1kVhHiHh