1.创建
static dispatch_source_t _reconTimer;
dispatch_source_cancel(_timer);
NSTimeInterval period =5;//设置时间间隔
dispatch_queue_t queue =dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);
_reconTimer =dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER,0,0, queue);
dispatch_source_set_timer(_reconTimer,dispatch_walltime(NULL,0), period * NSEC_PER_SEC,0);
dispatch_source_set_event_handler(_reconTimer, ^{
//方法
});
dispatch_resume(_reconTimer);
2.取消
dispatch_source_cancel(_reconTimer);

本文介绍如何使用Grand Central Dispatch (GCD)在Objective-C中创建一个定时器,并展示了如何设置时间间隔及事件处理函数。此外,还提供了取消定时器的方法。
407

被折叠的 条评论
为什么被折叠?



