倒计时 dispatch_source_t

-(void)timeCountDown

{

__blockinttimeout =59;

dispatch_queue_tque  =dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);

timer=dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER,0,0, que);

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);

dispatch_async(dispatch_get_main_queue(), ^{

//设置界面的按钮显示根据自己需求设置

[_getCodeButsetTitle:@"获取验证码"forState:UIControlStateNormal];

[_getCodeButsetBackgroundImage:[UIImageimageNamed:@"smallButBG"]forState:UIControlStateNormal];

_getCodeBut.userInteractionEnabled=YES;

});

}else{

intseconds = timeout %60;

NSString*strTime = [NSStringstringWithFormat:@"%d", seconds];

dispatch_async(dispatch_get_main_queue(), ^{

//设置界面的按钮显示根据自己需求设置

[_getCodeButsetTitle:[NSStringstringWithFormat:@"%@秒",strTime]forState:UIControlStateNormal];

//[_getAuthCodeBtn setBackgroundImage:[UIImage imageNamed:@"cert_gray_but.png"] forState:UIControlStateNormal];

_getCodeBut.userInteractionEnabled=NO;

});

timeout--;

}

});

dispatch_resume(timer);

}

你可能感兴趣的:(倒计时 dispatch_source_t)