Verification Code Countdown button application is very common, the blog and you together to achieve the verification Code countdown effect, define a Send Verification Code button, add click event, specific content as follows
Specific code:
Define a button to send the verification code, add the Click event
Send Verification Code button
_sentcodebtn = [[UIButton alloc] Initwithframe:cgrectmake (Kscreenwidth-27-4-A, Cgrectgetminy (_regist Ercodefd.frame) + 4;
[_sentcodebtn Setbackgroundcolor:colorwithrgba (0, 191, 191, 0.9)];
[_sentcodebtn settitle:@ "Send authentication Code" Forstate:uicontrolstatenormal];
[_sentcodebtn.titlelabel Setfont:[uifont systemfontofsize:13.0f]];
Set rounded corners
[_sentcodebtn.layer setcornerradius:3.0f];
[_sentcodebtn.layer Setshouldrasterize:yes];
[_sentcodebtn.layer Setrasterizationscale:[uiscreen Mainscreen].scale];
Send Event
[_sentcodebtn addtarget:self Action: @selector (Sentcodemethod) forControlEvents: UIControlEventTouchUpInside];
[Self.view ADDSUBVIEW:_SENTCODEBTN];
Listener event:
Send authentication Code-(void) sentcodemethod{NSLog (@) to send the authentication code ...
");
The timer sends the verification code [self Sentphonecodetimemethod];
Call Send authentication code interface-"}//Timer send verification code-(void) sentphonecodetimemethod{//Countdown time-60 seconds __block Nsinteger TimeOut = 59;
Execution Queue dispatch_queue_t queue = Dispatch_get_global_queue (Dispatch_queue_priority_default, 0); Timer-"Dispatch_source_set_timer automatic generation dispatch_source_t timer = dispatch_source_create (dispatch_source_type_timer, 0
, 0, queue);
Dispatch_source_set_timer (Timer, Dispatch_time_now, 1.0 * nsec_per_sec, 0 * nsec_per_sec);
Dispatch_source_set_event_handler (Timer, ^{if (timeOut <= 0) {dispatch_source_cancel (timer); Main thread Set button style-"Dispatch_async" (Dispatch_get_main_queue (), ^{[_sentcodebtn settitle:@ "Send authentication Code" Forstate:uicontrolstaten
Ormal];
[_sentcodebtn Setuserinteractionenabled:yes];
});
}else{//Start Timer//remaining seconds seconds nsinteger seconds = timeOut% 60;
NSString *strtime = [NSString stringwithformat:@ "%.1ld", seconds]; Main thread Set button style Dispatch_async (disPatch_get_main_queue (), ^{[UIView beginanimations:nil Context:nil];
[UIView setanimationduration:1.0];
[_sentcodebtn settitle:[nsstring stringwithformat:@ "%@s Resend", Strtime] forstate:uicontrolstatenormal];
[UIView commitanimations];
Timing devices are not allowed to click [_sentcodebtn Setuserinteractionenabled:no];
});
timeout--;
}
});
Dispatch_resume (timer);
}
The above is the entire content of this article, I hope to help you learn.