dispatch_source_t defaultTimer;///计时器
NSTimeInterval period = 1.0;
//创建队列
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
_defaultTimer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
dispatch_source_set_timer(_defaultTimer, dispatch_walltime(NULL, 0), period * NSEC_PER_SEC, 0);
__block NSInteger duration = defaultDuration;
dispatch_source_set_event_handler(_defaultTimer, ^{
dispatch_async(dispatch_get_main_queue(), ^{
if(duration==0)
{
dispatch_source_cancel(_defaultTimer);
[self remove];
}
duration--;
});
});
//取消队列
dispatch_source_set_cancel_handler(_defaultTimer, ^{
dispatch_release(_defaultTimer);
});
dispatch_resume(_defaultTimer);
网友评论