__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);
时间: 2024-10-15 14:00:42