Super simple stopwatch: Includes: Start, pause (unclear 0), clear 0 method
Core code
////VIEWCONTROLLER.M//Miaobiao////Created by Ibokan on 15/8/18.//Copyright (c) 2015 crazy. All rights reserved.//#import "ViewController.h"@interfaceViewcontroller () {int_alltime;} @property (nonatomic,strong) Uicolor* ENABLEDCOLOR;//Define global Press button color@property (Nonatomic,strong) UILabel *label;//Time display label@property (Nonatomic,strong) UIButton *buttonstart;//Start button@property (Nonatomic,strong) UIButton *buttonpause;//pause button@property (Nonatomic,strong) UIButton *buttonclear;//Qing 0@property (Nonatomic,strong) Nstimer *timer;//Timer@property (Nonatomic,strong) UILabel *labelinfo;//Author Information@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; //additional setup after loading the view, typically from a nib._enabledcolor = [Uicolor colorwithred: -/255.0Green198/255.0Blue233/255.0Alpha1];//Initialize Global colors//Initial change display labelSelf.label = [[UILabel alloc]initwithframe:cgrectmake (0,0, the, $)]; Self.label.backgroundColor=_enabledcolor; Self.label.text=@"00:00:00"; Self.label.font= [Uifont fontwithname:@"Helvetica"Size -]; Self.label.textColor=[Uicolor Whitecolor]; Self.label.textAlignment=Nstextalignmentcenter; [Self.view AddSubview:self.label]; //Initialize Start buttonSelf.buttonstart = [[UIButton alloc]initwithframe:cgrectmake (0, About,155, Max)]; [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]; //Initialize pause buttonSelf.buttonpause = [[UIButton alloc]initwithframe:cgrectmake (165, About,155, Max)]; [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]; //Initialize the purge buttonSelf.buttonclear = [[UIButton alloc]initwithframe:cgrectmake (0,370, the, Max)]; [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]; //initialization Information Display labelSelf.labelinfo = [[UILabel alloc]initwithframe:cgrectmake (0,530, the, +)]; Self.labelinfo.backgroundColor=_enabledcolor; Self.labelinfo.text=@"developed by Crazy van"; Self.labelinfo.textColor=[Uicolor Whitecolor]; Self.labelinfo.textAlignment=Nstextalignmentcenter; [Self.view AddSubview:self.labelinfo]; [Self clear];//call the Clear method to initialize the component}- (void) start{[Self.buttonstart settitle:@"Start"Forstate:uicontrolstatenormal];//Change the Start button display character (prepares for post-pause initialization)[self startunenabled]; [Self pauseenabled]; [Self clearenabled]; //the timer initializes where it needs to be called,Self.timer = [Nstimer scheduledtimerwithtimeinterval:0.01target:self Selector: @selector (ADD) Userinfo:nil Repeats:yes];}- (void) pause{[Self.buttonstart settitle:@"Continue"Forstate:uicontrolstatenormal];//Change the Start button display character[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/ -, _alltime% -]; //self.label.text = [[[NSString alloc]initwithformat:@ "%02d:%02d:%02d", _alltime/6000,_alltime%6000/100,_alltime %100] stringbyappendingstring:[[nsstring alloc]initwithformat:@ "%@", [[NSDate Alloc]init]]; }//Here is the button availability control method- (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 the can be recreated.}@end
View Code
Learning points
1, [uicolor colorwithred : 20/255.0 green :198/255.0 blue:233/255.0 alpha:1]; 
< Span class= "S3" >//OC Custom Colors
< Span class= "S3" > < Span class= "S1" > 2, self. Timer = [nstimer Scheduledtimerwithtimeinterval:0.01 target< span class= "s2" >:self selector: @selector (add) userinfo: Nil repeats:yes];
< span class= "s2" > //nstimer How to use
< span class= "s2" > < Span class= "S1" > 3, [self. Buttonstart addTarget:self action:@selector(start) forcontrolevents :uicontroleventtouchupinside];
callback method
Click I download source code
IOC development Learning-Simple timers (based on iPhone5 screen size development)