IOS Implementation Verification Code countdown function (b) _ios

Source: Internet
Author: User

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.

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.