Nstimer's use of the iphone timer function mimics

Source: Internet
Author: User

1 program Run Effect: 1.1 program starts the first interface, waits for the user to select the countdown time

1.2 After the user chooses the countdown time, starts the countdown.

1.3 Accept user Cancel, pause, continue etc interrupt

2 Design Process 2.1 Drag out a date picker control, two buttons, associate property variables, and then add secondary property variables, as follows:
1 @interfaceViewcontroller ()2 3@property (Weak, nonatomic) Iboutlet Uidatepicker *Timepicker;4@property (Weak, nonatomic) Iboutlet UIButton *pauseandcontinuebtn;5 6@property (Weak, nonatomic) UILabel *Timerlabel;7@property (Weak, nonatomic) Nstimer *Counttimer;8 @property (Assign, nonatomic) Nsinteger second;9 Ten @end
2.2 The response function of the Start Timer button is:
1-(Ibaction) Countstart: (UIButton *) countbtn {2     3     if([CountBtn.titleLabel.text isequaltostring:@"Start Timing"])4     {5self.pauseAndContinueBtn.enabled =YES;6         7UILabel *timelabel =[[UILabel alloc] init];8Timelabel.frame =Self.timePicker.frame;9 TenNsinteger second =Self.second; OneTimelabel.text = [NSString stringWithFormat:@"%02ld:%02ld:%02ld", second/3600, second%3600/ -, second% -]; ATimelabel.font = [Uifont systemfontofsize: -]; -Timelabel.textalignment =Nstextalignmentcenter; -  theSelf.timePicker.alpha =0.0f; - [Self.view Addsubview:timelabel]; -  -Self.timerlabel =TimeLabel; +[Countbtn Settitle:@"Cancel"Forstate:uicontrolstatenormal]; -  +Self.counttimer = [Nstimer scheduledtimerwithtimeinterval:1.0ftarget:self selector: @selector (countstartsubforupdatelabeltext) Userinfo:nil Repeats:yes]; A [[Nsrunloop Mainrunloop] AddTimer:self.countTimer formode:nsrunloopcommonmodes]; at     } -     Else -     { -self.pauseAndContinueBtn.enabled =NO; -[Self.pauseandcontinuebtn Settitle:@"Pause"Forstate:uicontrolstatenormal]; -          in [Self.timerlabel Removefromsuperview]; -Self.timePicker.alpha =1.0f; to          +[Countbtn Settitle:@"Start Timing"Forstate:uicontrolstatenormal]; - [Self.counttimer invalidate]; the          * [self countDown:self.timePicker]; $     } the}
2.3 The response function of the Pause continuation button is:
1-(Ibaction) Countpause: (UIButton *) pausebtn2 {3     if([PauseBtn.titleLabel.text isequaltostring:@"Pause"])4     {5[Pausebtn Settitle:@"continue to"Forstate:uicontrolstatenormal];6 [Self.counttimer setfiredate:[nsdate distantfuture];7     }8     Else9     {Ten[Pausebtn Settitle:@"Pause"Forstate:uicontrolstatenormal]; One [Self.counttimer setfiredate:[nsdate distantpast]; A     } -}
The 2.4 date picker receives the user-selected function from the interface and writes the result to a property variable holding the number of seconds, with the following code:
1- (void) Countdown: (Uidatepicker *) Timepicker2 {3 4NSDateFormatter *mydate =[[NSDateFormatter alloc] init];5[MyDate Setdateformat:@"HH:mm:ss"];6NSString *stringtime =[MyDate stringFromDate:timePicker.date];7     8Nsarray *hourandmin = [StringTime componentsseparatedbystring:@":"];9NSString *stringhour = hourandmin[0];TenNSString *stringmin = hourandmin[1]; One      ANsinteger hour =Stringhour.integervalue; -Nsinteger min =Stringmin.integervalue; -Nsinteger second = hour*3600+min* -; the      -Self.second =second; -  -}
2.5 loading content in the Viewdidload function:
1 -(void) viewdidload {2    [Super Viewdidload]; 3     4     Self.timePicker.datePickerMode = Uidatepickermodecountdowntimer; 5     [Self.timepicker addtarget:self Action: @selector (Countdown:) forControlEvents: Uicontroleventvaluechanged]; 6     7     self.pauseAndContinueBtn.enabled = NO; 8 }
2.6 Countdown Label control content Refresh function:
1 -(void) countstartsubforupdatelabeltext2{3     self.second--; 4     Self.timerLabel.text = [NSString stringWithFormat:@ "%02ld:%02ld:%02ld", self.second/3600, self.second%3600/A, self.second%]; 5 }
3 Note that the date Picker control property is set to Countdown mode.

4 just have a try,you would find more ...

Nstimer's use of the iphone timer function emulation

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.