GCDTimer
使用 GCD 实现的计时器。这是一种优雅地使用 GCD 实现计时器的方法,更简单,我们无需关心在不同线程中使用 NSTimer 的使用。
使用方法
- 将 GCDTimer.h 和 GCDTimer.m 文件导入到您的项目中。
- 将 GCDTimer.h 导入到您想要使用的文件中,并按照以下方式调用。
_timer = [GCDTimer scheduleTimerWithInterval:1 repeats:YES isMainQueue:NO block:^{
NSLog(@"testViewController globalQueue executed");
}];
注意:当 repeats = YES 时,您必须调用方法
- (void)invalidate;
来释放 GCDTimer 实例。
API
/**
create a GCDTimer instance, Selector version
@param interval
@param repeats Whether repeat
@param isMainQueue Whether execute the block on the main queue
@param target The target to perform the selector
@param selector The selector will to excute
@return GCDTimer instance
NOTE: when repeats = YES, you must invoke the method `- (void)invalidate;` to release the GCDTimer instance.
*/
+ (instancetype)scheduleTimerWithInterval:(NSTimeInterval) interval repeats:(BOOL)repeats isMainQueue:(BOOL)isMainQueue target:(id)target selector:(SEL)selector;
/**
create a GCDTimer instance, Block version
@param interval
@param repeats Whether repeat
@param isMainQueue Whether execute the block on the main queue
@param block The block to invoke
@return GCDTimer instance
NOTE: when repeats = YES, you must invoke the method `- (void)invalidate;` to release the GCDTimer instance.
*/
+ (instancetype)scheduleTimerWithInterval:(NSTimeInterval) interval repeats:(BOOL)repeats isMainQueue:(BOOL)isMainQueue block:(dispatch_block_t)block;
/**
cancel timer
*/
- (void)invalidate;
...