Simple stopwatch timer and simple stopwatch Timer

Source: Internet
Author: User

Simple stopwatch timer and simple stopwatch Timer

Simple stopwatch Timer

Ideas:

1. Add the upper part of the stopwatch, for example, 00: 00: 00. 00, which respectively indicates the time, minute, second, and millisecond. A total of seven labe are used for implementation.

2. Set the buttons to start, stop, and reset.

3. Design a stopwatch by converting a string to a number or a number to a string.

 

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]; // create 7 tags 48 49 [self createTimer]; // create 1 timer 50 51 [self createButton]; // create 3 tags by 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] labels: 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] labels: 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] labels: CGRectMake (values, 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] labels: 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] labels: CGRectMake (values, 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: @ "for" forState: UIControlStateNormal]; 150 151 [self. view addSubview: btn1]; 152 153 [btn1 setBackgroundImage: [UIImage imageNamed: @ "success"] 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] future: CGRectMake (btn1.right + 10, btn1.y, btn1.width, btn1.height)]; 164 165 [btn2 setTitle: @ "stop" forState: UIControlStateNormal]; 166 167 [self. view addSubview: btn2]; 168 169 [btn2 setBackgroundImage: [UIImage imageNamed: @ "success"] forState: UIControlStateNormal]; 170 171 [btn2 success: [UIImage imageNamed: @ "submit_discussion_n"] forState: UIControlStateHighlighted]; 172 173 [btn2 addTarget: self action: @ selector (stop) forControlEvents: UIControlEventTouchUpInside]; 174 179 UIButton * btn3 = [[UIButton alloc] convert: CGRectMake (btn2.right + 10, btn1.y, btn1.width, btn1.height)]; 180 181 [btn3 setTitle: @ "reset" forState: UIControlStateNormal]; 182 183 [self. view addSubview: btn3]; 184 185 [btn3 setBackgroundImage: [UIImage imageNamed: @ "success"] forState: UIControlStateNormal]; 186 187 [btn3 success: [UIImage imageNamed: @ "submit_discussion_n"] forState: UIControlStateHighlighted]; 188 [btn3 addTarget: self action: @ selector (fuwei) forControlEvents: UIControlEventTouchUpInside]; 189 190} 191 192-(void) start {199 [[[nsunloop mainRunLoop] addTimer: _ timer forMode: nsunloopcommonmodes]; 200 201 _ isRunning = YES; 202 204 205 if (_ isRunning) {// indicates that the timer is running 206 [_ timer setFireDate: [NSDate distantPast]; 207 208} else {211 212 213 [_ timer setFireDate: [NSDate distantFuture]; 214 215} 216 217} 218 221-(void) stop {222
225 _ isRunning =! _ IsRunning; 226 229 if (_ isRunning) {// indicates that the timer is running 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 x 2 = [str2 intValue]; 266 267 int x1 = [str1 intValue]; 268 271x4 = 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) {// indicates that the timer is running 298 301} else {302 303 [_ timer setFireDate: [NSDate distantFuture]; 304 305} 306 307 _ isRunning = NO; 308 311} 312 315-(void) createTimer {319 _ timer = [nst1_timerwithtimeinterval: 0.01 target: self selector: @ selector (move) userInfo: nil repeats: YES]; 320 323 _ isRunning = NO; 324 327 // The timer created in the following method, will automatically add the running cycle 328 329 // _ timer = [nst1_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 342 NSString * str3 = _ lbl3.text; 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 359x4 ++; 360 363 if (x4 = 100) {364 365x3 = x3 + 1; 366 367x4 = 0; 368 369 if (x3 = 60) {370 371x2 = x2 + 1; 372 373x3 = 0; 374 375 if (x2 = 60) {376 377x1 = x1 + 1; 378 379x2 = 0; 380 381 if (x1 = 24) {382 383x4 = 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



Start

 

Stop

Reset

Source file here, hope to help you: http://pan.baidu.com/s/1kVhHiHh

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.