method One : Use nstimer to achieve (comparison is applicable to send SMS Verification code countdown)
Mainly using Nstimer's Scheduledtimerwithtimeinterval method to execute Changetime method per second
Create a timer to execute Changetime once per second: method
Nstimer * Timer =[nstimer scheduledtimerwithtimeinterval:1.0 target:self selector: @selector (changetime:) userinfo:nil Repeats:yes];
Changetime
-(void) Changetime: (nstimer*) Timer
{
Click to get the btn of the Verification code
UIButton * button = (uibutton*) [Self.view viewwithtag:99];
if (count = = 0) {
Invalidate out after completion
[Timer invalidate];
59s Countdown
Count = 59;
[Button settitle:@ "regain" forstate:uicontrolstatenormal];
button.userinteractionenabled = YES;
Button.alpha = 1;
}
else{
[Button settitle:[nsstring stringwithformat:@ "%d S", Count] forstate:uicontrolstatenormal];
count--;
}
}
method Two : Use GCD to achieve (compare the countdown to use for the merchant to do some sort of activity)
A timer is defined in the. h file to control the time
Countdown timer
dispatch_source_t _timer;
Implemented in. m Files:
Create a timestamp
NSDateFormatter *dateformatter=[[nsdateformatter alloc] init];
Format of time stamps
[Dateformatter setdateformat:@ "Yyyy/mm/dd HH:mm:ss"];
Convert data of type nsstring of the same timestamp format to nsdate type data
NSDate *enddate = [Dateformatter datefromstring:_endtime];
NSDate *startdate = [Dateformatter datefromstring:_starttime];
NSDate *currentdate = [Dateformatter datefromstring:_currenttime];
Calculates the timestamp of the end of the server's current time distance activity
Nstimeinterval timeinterval =[enddate Timeintervalsincedate:currentdate];
Calculates the timestamp of the current time of the server and the start time of the activity
Nstimeinterval Starttonow = [currentdate timeintervalsincedate:startdate];
Countdown time
__block int timeout = timeinterval;
__block int starttimeout = Starttonow;
if (timeout!=0) {
dispatch_queue_t queue = Dispatch_get_global_queue (Dispatch_queue_priority_default, 0);
Parallel queues
Parallel queues can handle multiple tasks at the same time, and can be created with dispatch_queue_create without being able to do so, but generally we use a system-predefined parallel queue, global queue (globally/Concurrent Dispatch Queues). Currently, the system pre-defines four global queues with different operational priorities, which we can obtain through dispatch_get_global_queue.
Dispatch_get_global_queue The first parameter is the priority of the queue and corresponds to four global queues, respectively:
Dispatch_queue_priority_high
Dispatch_queue_priority_default
Dispatch_queue_priority_low
Dispatch_queue_priority_background
The second parameter in Dispatch_get_global_queue is currently reserved, please set it to 0.
Executes per second
_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);
Dispatch_source_set_event_handler (_timer, ^{
Countdown End, close
if (timeout<=0 | | Starttimeout <=0) {
Dispatch_source_cancel (_timer);
_timer = nil;
Run a task in a queue
You can add a new task to a queue at any time, just call Dispatch_async:
Dispatch_async (Dispatch_get_main_queue (), ^{
According to their own needs to design the content to display and show the format, style, etc.
Daylabel.text = @ "0 days";
Hourlabel.text = @ "00:";
Minlabel.text = @ "00:";
Seclabel.text = @ "00";
Label.text = @ "snapping ends!!!" ";
});
}else{
Label.text = @ "Snapping up time remaining:";
int days = (int) (timeout/(3600*24));
if (days==0) {
Daylabel.text = @ "";
}
int hours = (int) ((timeout-days*24*3600)/3600);
int minute = (int) (timeout-days*24*3600-hours*3600)/60;
int second = timeout-days*24*3600-hours*3600-minute*60;
Dispatch_async (Dispatch_get_main_queue (), ^{
if (days==0) {
Daylabel.text = @ "0 days";
}else{
Daylabel.text = [NSString stringwithformat:@ "%d Day", days];
}
if (hours<10) {
Hourlabel.text = [NSString stringwithformat:@ "0%d:", hours];
}else{
Hourlabel.text = [NSString stringwithformat:@ "%d:", hours];
}
if (minute<10) {
Minlabel.text = [NSString stringwithformat:@ "0%d:", minute];
}else{
Minlabel.text = [NSString stringwithformat:@ "%d:", minute];
}
if (second<10) {
Seclabel.text = [NSString stringwithformat:@ "0%d", second];
}else{
Seclabel.text = [NSString stringwithformat:@ "%d", second];
}
});
timeout--;
}
});
Dispatch_resume (_timer);
}
Countdown in iOS