IOS countdown and iOS countdown
Method 1: Use NSTimer (applicable to message Verification Code countdown)
The NSTimer scheduledTimerWithTimeInterval method is used to execute the changeTime method once per second.
// Create a Timer and execute changeTime every second: Method
NSTimer * timer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target: self selector: @ selector (changeTime :) userInfo: nil repeats: YES];
// ChangeTime
-(Void) changeTime :( nstmer *) timer
{
// Click btn to obtain the verification code
UIButton * button = (UIButton *) [self. view viewWithTag: 99];
If (count = 0 ){
// Invalidate after completion
[Timer invalidate];
// 59s countdown
Count = 59;
[Button setTitle: @ "re-obtain" forState: UIControlStateNormal];
Button. userInteractionEnabled = YES;
Button. alpha = 1;
}
Else {
[Button setTitle: [NSString stringWithFormat: @ "% d s", count] forState: UIControlStateNormal];
Count --;
}
}
Method 2: GCD is used for implementation (compared to the countdown of a seller to a certain activity)
In the. h file, define a Timer to control the time.
// Countdown Timer
Dispatch_source_t _ timer;
Implementation in the. m file:
// Create a timestamp
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
// Timestamp format
[DateFormatter setDateFormat: @ "yyyy/MM/dd HH: mm: ss"];
// Convert NSString data of the same timestamp format to NSDate data
NSDate * endDate = [dateFormatter dateFromString: _ EndTime];
NSDate * startDate = [dateFormatter dateFromString: _ StartTime];
NSDate * currentDate = [dateFormatter dateFromString: _ CurrentTime];
// Calculate the timestamp of the current server time from the end of the activity
NSTimeInterval timeInterval = [endDate timeIntervalSinceDate: currentDate];
// Calculate the timestamp of the current server time and Activity start time.
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 queue
// The parallel queue can process multiple tasks at the same time. You can use dispatch_queue_create to create a parallel queue, but we usually use the predefined parallel queue, that is, Global Queue (Global // Concurrent Dispatch Queues ). Currently, the system predefines four global Queues with different running priorities. We can obtain them through dispatch_get_global_queue.
// The first parameter of dispatch_get_global_queue is the queue priority, which 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 retained by the system. Set it to 0.
// Execution per second
_ Timer = dispatch_source_create (DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue );
Dispatch_source_set_timer (_ timer, dispatch_wall time (NULL, 0), 1.0 * NSEC_PER_SEC, 0 );
Dispatch_source_set_event_handler (_ timer, ^ {
// The countdown is over and off
If (timeout <= 0 | StartTimeout <= 0 ){
Dispatch_source_cancel (_ timer );
_ Timer = nil;
// Run the task in the queue
// You can add a new task to a queue at any time. You only need to call dispatch_async:
Dispatch_async (dispatch_get_main_queue (), ^ {
// You can design the content, display format, and style to be displayed as needed.
DayLabel. text = @ "0 days ";
HourLabel. text = @ "00 :";
MinLabel. text = @ "00 :";
SecLabel. text = @ "00 ";
Label. text = @ "The Flash sale is over !!! ";
});
} Else {
Label. text = @ "Remaining Time for flash sale :";
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 x 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 days", 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 );
}