簡單的秒錶定時器,簡單秒錶定時器

來源:互聯網
上載者:User

簡單的秒錶定時器,簡單秒錶定時器

 簡單的秒錶定時器

思路:

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

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.