Apple development of iOS verification code countdown

Source: Internet
Author: User

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:

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.