__blockintTimeout = -;//Countdown Timedispatch_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), Nsec_per_sec *1.0,0);//executes per secondDispatch_source_set_event_handler (Timer, ^{ if(Timeout <=0) {dispatch_source_cancel (timer); Dispatch_async (Dispatch_get_main_queue (),^{Cnslog (@"Get Verification Code"); Self.getVerificationCodeButton.enabled=YES; Self.getVerificationCodeLabel.text=@"Get Verification Code"; Self.getVerificationCodeLabel.textColor=[Uicolor Whitecolor]; }); } Else{nsstring* Strtime = [NSString stringWithFormat:@"Please wait a moment (%d seconds)", timeout]; Dispatch_async (Dispatch_get_main_queue (),^{Cnslog (@"Timeout =%d", timeout); Self.getVerificationCodeButton.enabled=NO; Self.getVerificationCodeLabel.textColor= Uicolorfromrgb (0xe6e6e6); Self.getVerificationCodeLabel.text=Strtime; }); Timeout-- ; } }); Dispatch_resume (timer);
Original: http://blog.csdn.net/fanxun1982/article/details/8678362
iOS uses GCD to display countdown