@property(nonatomic,retain) dispatch_source_t timer;
__block int timeout = 5; //倒计时时间 自定义
__weak __typeof(&*self)weakself = self; //防止引用,先弱下
if (timeout!=0) {
//GCD定时器
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
_timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每1秒执行
dispatch_source_set_event_handler(_timer, ^{
if(timeout<=0){ //倒计时结束,关闭
dispatch_source_cancel(_timer);
_timer = nil;
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"倒计时结束操作");
});
}else{
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"这里显示倒计时时间==%d",timeout);
});
timeout--;
}
});
dispatch_resume(_timer);
}
//退出页面
dispatch_source_cancel(_timer);