倒计时按钮

//倒计时async

- (void) reciprocal {spa

    //按钮倒计时orm

     __block NSInteger timeOut = 60;//倒计时时间ip

    

    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);ci

    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);get

    dispatch_source_set_timer(_timer, dispatch_walltime(NULL, 0), 1*NSEC_PER_SEC, 0);//每秒执行string

 

    dispatch_source_set_event_handler(_timer, ^{it

        if (timeOut <= 0) {io

            dispatch_source_cancel(_timer);event

            dispatch_async(dispatch_get_main_queue(), ^{

                //设置时间

                [self.getCodeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];

                self.getCodeBtn.userInteractionEnabled = true;

            });

        } else {

            NSString  *strTime = [NSString stringWithFormat:@"%ld 秒",(long)timeOut];

            dispatch_async(dispatch_get_main_queue(), ^{

                [self.getCodeBtn setTitle:strTime forState:UIControlStateNormal];

            });

            timeOut--;

        }

    });

    dispatch_resume(_timer);

}

相关文章
相关标签/搜索