iOS Development Diary 46-Countdown Effect implementation

Source: Internet
Author: User

Today Bo Master has a countdown effect of the realization of the demand, encountered some difficulties, here and we share, hope to progress together.

First in need of the place to resolve the total countdown time, Bo Master no package, you crossing can self-encapsulation

#pragma mark--------New revisions, added countdown

NSString *stringoftime=[nsstring stringwithformat:@ "%@", d[@ "Shipping_time"];

Double endunixtime = [Stringoftime doublevalue];

NSLog (@ "++++++%.2f", endunixtime);

Nscalendar *cal = [Nscalendar Currentcalendar];

unsigned int unitflags = Nsyearcalendarunit | Nsmonthcalendarunit | Nsdaycalendarunit | Nshourcalendarunit | Nsminutecalendarunit | Nssecondcalendarunit;

NSDate *date1 = [NSDate Date];

NSDate *date2 = [NSDate datewithtimeintervalsince1970:endunixtime];

Nsdatecomponents *DOFD = [cal components:unitflags fromdate:date1 Todate:date2 options:0];

Nsinteger sec = [DofD hour]*3600+[dofd minute]*60+[dofd Second];

SEC is the total number of minutes

NSLog (@ "**********%@", DOFD);

UILabel *labeloftime= (UILabel *) [_scroll viewwithtag:6954321];

if (sec>0) {

[Self setcountdownwithtotalsec:sec withlabel:labeloftime];

}

Then use GCD to implement the countdown function

-(void) Setcountdownwithtotalsec: (Nsinteger) totalsec Withlabel: (UILabel *) timelbl{

__block Nsinteger timeout = totalsec; Countdown time

dispatch_queue_t queue = Dispatch_get_global_queue (Dispatch_queue_priority_default, 0);

dispatch_source_t _timer = dispatch_source_create (dispatch_source_type_timer, 0, 0,queue);

Dispatch_source_set_timer (_timer,dispatch_walltime (NULL, 0), 1.0*nsec_per_sec, 0); Executes per second

Dispatch_source_set_event_handler (_timer, ^{

if (timeout<=0) {//Countdown end, close

Dispatch_source_cancel (_timer);

Dispatch_async (Dispatch_get_main_queue (), ^{

Set the interface button to display the settings according to your needs

Timelbl.text = @ "00:00:00";

});

}else{

Long hours = timeout/3600;

Long minutes = (timeout-hours * 3600)/60;

int seconds = timeout% 60;

NSString *strtime = [NSString stringwithformat:@ "%.2ld%.2ld%.2d", hours,minutes, seconds];

NSString *firsth = [Strtime substringwithrange:nsmakerange (0, 1)];

NSString *SECONDH = [Strtime substringwithrange:nsmakerange (1, 1)];

NSString *firstm = [Strtime substringwithrange:nsmakerange (2, 1)];

NSString *SECONDM = [Strtime substringwithrange:nsmakerange (3, 1)];

NSString *firsts = [Strtime substringwithrange:nsmakerange (4, 1)];

NSString *seconds = [Strtime substringwithrange:nsmakerange (5, 1)];

NSString *newtimestring = [NSString stringwithformat:@ "%@%@:%@%@:%@%@", Firsth,secondh,firstm,secondm,firsts, SecondS];

Dispatch_async (Dispatch_get_main_queue (), ^{

Timelbl.text = newtimestring;//back to the main thread display

});

timeout--;

}

});

Dispatch_resume (_timer);

}

iOS Development Diary 46-Countdown Effect implementation

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.