使用GCD验证码倒计时

 __block int timeout = 60;
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0);
    
    dispatch_source_t source_t = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    
    dispatch_source_set_timer(source_t, dispatch_walltime(NULL, 0), 1*NSEC_PER_SEC, 0);
    
    dispatch_source_set_event_handler(source_t, ^{
        
        if (timeout<=0) {
            
            dispatch_source_cancel(source_t);
            dispatch_async(dispatch_get_main_queue(), ^{
                
                NSLog(@"重新开始倒计时");
            });
            
            
        }
        else
        {
            dispatch_async(dispatch_get_main_queue(), ^{
                
                NSLog(@"开始倒计时剩余%.2ds",timeout%60);
            });
        
            timeout --;
            
            
        }
        
        
        
    });
    dispatch_resume(source_t);
    
    
    
    
    
    

  

posted @ 2016-03-22 11:23  曹县三胖暴打大猩猩  阅读(229)  评论(0编辑  收藏  举报