zyviewcontroller.m//selectortime////Created by Mac on 15-8-3.//Copyright (c) 2015 zhiyou. All rights reserved.//#import "ZYViewController.h" @interface Zyviewcontroller () {//global variable Nstimer *timer;} @end @implementation zyviewcontroller-(ID) initwithnibname: (NSString *) Nibnameornil Bundle: (NSBundle *) nibbundleornil{self = [super Initwithnibname:nibnameornil Bundle:nibbundleornil]; if (self) {//Custom initialization} return to self;} Idea//1 Create 5 labels to display numbers and colons//2 turn on timer (set three counters: seconds counter, minute counter, hour counter) use the concatenation string method to make the numbers appear on the label;//3 create nstimer-(void) viewdidload{[Super Viewdidload]; UILabel *label1=[[uilabel Alloc] Initwithframe:cgrectmake (40, 100, 60, 60)]; [email protected] "00"; label1.tag=1; Label1.backgroundcolor=[uicolor Bluecolor]; Label1.textalignment=nstextalignmentcenter; Label1.textcolor=[uicolor Whitecolor]; [Self.view Addsubview:label1]; UILabel *label2=[[uilabel Alloc] initwithframe:cgrectmake (100, 100, 10, 60)]; [email protected] ":"; [Self.view Addsubview:label2]; UILabel *label3=[[uilabel Alloc] Initwithframe:cgrectmake (110, 100, 60, 60)]; [email protected] "00"; label3.tag=3; Label3.backgroundcolor=[uicolor Bluecolor]; Label3.textcolor=[uicolor Whitecolor]; Label3.textalignment=nstextalignmentcenter; [Self.view Addsubview:label3]; UILabel *label4=[[uilabel Alloc] Initwithframe:cgrectmake (170, 100, 10, 60)]; [email protected] ":"; [Self.view Addsubview:label4]; UILabel *label5=[[uilabel Alloc] initwithframe:cgrectmake (180, 100, 60, 60)]; [email protected] "00"; label5.tag=5; Label5.backgroundcolor=[uicolor Bluecolor]; Label5.textalignment=nstextalignmentcenter; Label5.textcolor=[uicolor Whitecolor]; [Self.view Addsubview:label5]; Turn on timer timer=[nstimer scheduledtimerwithtimeinterval:1 target:self selector: @selector (onTimer) Userinfo:nil repeat S:yes]; Btn1=[uibutton ButtonwithtyPe:uibuttontypecustom]; Btn1.frame=cgrectmake (50, 300, 50, 50); Btn1.backgroundcolor=[uicolor Redcolor]; [Btn1 settitle:@ "pause" forstate:uicontrolstatenormal]; [Btn1 addtarget:self Action: @selector (click) forcontrolevents:uicontroleventtouchupinside]; [Self.view ADDSUBVIEW:BTN1]; Btn2=[uibutton Buttonwithtype:uibuttontypecustom]; Btn2.frame=cgrectmake (130, 300, 50, 50); Btn2.backgroundcolor=[uicolor Redcolor]; [btn2 settitle:@ "Qing 0" forstate:uicontrolstatenormal]; [Btn2 addtarget:self Action: @selector (OnClick) forcontrolevents:uicontroleventtouchupinside]; [Self.view ADDSUBVIEW:BTN2]; }-(void) click{open=!open; if (open==yes) {[btn1 settitle:@ ' start ' forstate:uicontrolstatenormal]; [Timer invalidate]; Timer=nil; }else{[btn1 settitle:@ "pause" forstate:uicontrolstatenormal]; timer= [Nstimer scheduledtimerwithtimeinterval:1 target:self selector: @selector (OnTimer) Userinfo:nil Repeats:yes]; }}-(Void) onclick{UILabel *label1= (UILabel *) [Self.view viewwithtag:1]; UILabel *label3= (UILabel *) [Self.view Viewwithtag:3]; UILabel *label5= (UILabel *) [Self.view viewwithtag:5]; [email protected] "00"; scount=mcount=hcount=0;} -(void) ontimer{//viewwithtag through the tag to find the control//must be found in the parent view UILabel *label1= (UILabel *) [Self.view viewwithtag:1]; UILabel *label3= (UILabel *) [Self.view Viewwithtag:3]; UILabel *label5= (UILabel *) [Self.view viewwithtag:5]; Change the title scount++; if (scount==59) {scount=0; mcount++; [Self setlabelanimations:label3]; Label3.text=[nsstring stringwithformat:@ "%d", MCount]; if (mcount==59) {mcount=0; hcount++; [Self setlabelanimations:label1]; Label1.text=[nsstring stringwithformat:@ "%d", Hcount]; }} label5.text=[nsstring stringwithformat:@ "%d", Scount]; 2 animating (label animating, uploading labels to animations) [self setlabelanimations:label5]; }-(void) Setlabelanimations: (UILabel *) labels{[UIView Beginanimations:nil Context:nil]; [UIView setanimationduration:0.9]; [UIView Setanimationtransition:6 forview:labels Cache:yes]; [UIView commitanimations];} -(void) didreceivememorywarning{[Super didreceivememorywarning]; Dispose of any resources the can be recreated.} @end
Material Source: http://download.csdn.net/detail/swanzhu/8957883
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
(material source) Swanzhu iOS (ii) UI _nstimer