The first step, drag two space textfiled and button to the storyboard on the Viewcontroller.
The second step, drag the line, link to the. h file in the following code:
@property (Weak, nonatomic) Iboutlet UIButton *l_timebutton;
In the third step, set the listener for L_timebutton in the M file and listen for the Click event.
-(void) Viewdidload {
[Super Viewdidload];
[_l_timebutton addtarget:self Action: @selector (StartTime) forcontrolevents:uicontroleventtouchupinside];
}
The fourth step, in the method "StartTime" to achieve the countdown function
-(void) starttime{
__block int timeout=30; 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
[_l_timebutton settitle:@ "Send authentication Code" Forstate:uicontrolstatenormal];
_l_timebutton.userinteractionenabled = YES;
});
}else{
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
NSLog (@ "____%@", strtime);
[UIView Beginanimations:nil Context:nil];
[UIView Setanimationduration:1];
[_l_timebutton settitle:[nsstring stringwithformat:@ "%@ seconds later Resend", Strtime] forstate:uicontrolstatenormal];
[UIView commitanimations];
_l_timebutton.userinteractionenabled = NO;
});
timeout--;
}
});
Dispatch_resume (_timer);
}
Well, a verification code countdown function is written. Also, another method, later when I study well, in the introduction to everyone. Attach the running picture as follows: