iOS development to achieve the product countdown effect

Source: Internet
Author: User

A. Countdown (e.g., send verification code)

#define COUNTDOWNTIME 119//Countdown time
#pragma mark-Countdown.
-(void) MyTimer
{
if (countdowntime) {
__block int timeout= countdowntime; 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); Execute per second
Dispatch_source_set_event_handler (Timer, ^{
if (Timeout < 0) {//countdown ended, off
Dispatch_source_cancel (timer);
Dispatch_async (Dispatch_get_main_queue (), ^{
Set the button of the interface to display according to your own needs
});
}else{
int hours = Timeout/(60 * 60);
int minute = timeout% (60 * 60)/60;
int seconds = timeout% 60;
if (Hours < 10) {
Hours = [[NSString stringwithformat:@"0%d",hours] intvalue];
}
if (minute < 10) {
minute = [[NSString Stringwithformat:@"0%d",minute] intvalue];
}
if (seconds < 10) {
seconds = [[NSString stringwithformat:@"0%d",seconds] intvalue];
}
Dispatch_async (Dispatch_get_main_queue (), ^{
Set the button of the interface to display according to your own needs
[UIView Beginanimations:nil Context:nil];
[UIView Setanimationduration:1];
Hourlabel.text = [NSString stringwithformat:@"%d",hours];
Minutelabel.text = [NSString stringwithformat:@"%d",minute];
Secondslabel.text = [NSString stringwithformat:@"%d",seconds];
if (Hours < 10) {
Hourlabel.text = [NSString stringwithformat:@"0%d",hours];
}
if (minute < 10) {
Minutelabel.text = [NSString stringwithformat:@"0%d",minute];
}
if (seconds < 10) {
Secondslabel.text = [NSString stringwithformat:@"0%d",seconds];
}
[UIView commitanimations];
});
timeout--;
}
});
Dispatch_resume (timer);
}
}

Two. Countdown (and calculate the time lag)

Global variable: dispatch_source_t _timer;
#pragma mark-Countdown.
-(void) countdown{
NSDate *now = [NSDate Date];
NSDateFormatter *formatday = [[NSDateFormatter alloc] init];
Formatday.dateformat = @"yyyy-mm-dd";
NSString *daystr = [Formatday Stringfromdate:now];

NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init];
[Dateformatter setdateformat:@"yyyy-mm-dd"];

NSDate *enddate = [Dateformatter datefromstring:daystr];
NSDate *enddate_tomorrow = [[NSDate alloc] initwithtimeintervalsincereferencedate: ([EndDate Timeintervalsincereferencedate] + 24 * 3600)];
NSDate *startdate = [NSDate Date];
Nstimeinterval timeinterval =[enddate_tomorrow Timeintervalsincedate:startdate];

if (_timer = = nil) {
__block int timeout = timeinterval; Countdown time
if (Timeout!= 0) {
dispatch_queue_t queue = Dispatch_get_global_queue (Dispatch_queue_priority_default, 0);
_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); Execute per second
Dispatch_source_set_event_handler (_timer, ^{
if (Timeout <= 0) {//countdown ended, off
Dispatch_source_cancel (_timer);
_timer = nil;
Dispatch_async (Dispatch_get_main_queue (), ^{
Hourlabel.text = @"00";
Minutelabel.text = @"00";
Secondslabel.text = @"00";
});
}else{
int hours = (int) (timeout/3600);
int minutes = (int) (timeout-hours * 3600)/60;
int seconds = timeout-hours * 3600-minutes * 60;
Dispatch_async (Dispatch_get_main_queue (), ^{
if (Hours < 10) {
Hourlabel.text = [NSString stringwithformat:@"0%d",hours];
}else{
Hourlabel.text = [NSString stringwithformat:@"%d",hours];
}
if (minutes < 10) {
Minutelabel.text = [NSString stringwithformat:@"0%d",minutes];
}else{
Minutelabel.text = [NSString stringwithformat:@"%d",minutes];
}
if (seconds < 10) {
Secondslabel.text = [NSString stringwithformat:@"0%d",seconds];
}else{
Secondslabel.text = [NSString stringwithformat:@"%d",seconds];
}

});
timeout--;
}
});
Dispatch_resume (_timer);
}
}
}

Example Three

__block int timeout=59; 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 (Aself.timer,dispatch_walltime (NULL, 0), 1.0*nsec_per_sec, 0); Execute per second
Dispatch_source_set_event_handler (Aself.timer, ^{
if (timeout<=0) {//countdown ended, off
Dispatch_source_cancel (Aself.timer);
Aself.timer = nil;
Dispatch_async (Dispatch_get_main_queue (), ^{
Set the button of the interface to display according to your own needs
});
}else{
int minutes = TIMEOUT/60;
int seconds = timeout% 60;
NSString *strtime = [NSString stringwithformat:@ "%.2d", seconds];
Dispatch_async (Dispatch_get_main_queue (), ^{
Set the button of the interface to display according to your own needs
});
timeout--;
}
});

Dispatch_resume (Aself.timer);

All right, the above is about the iOS countdown to a realization method, this and JS in the countdown is very close to oh you should understand it.

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.