话不多说,直接上代码
//
// SFJTimer.h
// 定时器-GCD
//
// Created by 宋法键 on 2019/8/14.
// Copyright © 2019 WZSG. All rights reserved.
//
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface SFJTimer : NSObject
+ (NSString *)executeTask:(void(^)(void))task start:(NSTimeInterval)start interval:(NSTimeInterval)interval repeat:(BOOL)repeat async:(BOOL)async;
+ (NSString *)executeTask:(id)target selector:(SEL)selector start:(NSTimeInterval)start interval:(NSTimeInterval)interval repeat:(BOOL)repeat async:(BOOL)async;
+ (void)cancelTask:(NSString *)taskSign;
@end
NS_ASSUME_NONNULL_END
//
// SFJTimer.m
// 定时器-GCD
//
// Created by 宋法键 on 2019/8/14.
// Copyright © 2019 WZSG. All rights reserved.
//
#import "SFJTimer.h"
//多线程操作,需要加锁
static NSMutableDictionary *timers_;
static dispatch_semaphore_t semaphore_t_;
@implementation SFJTimer
+ (void)initialize {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
timers_ = [NSMutableDictionary dictionary];
semaphore_t_ = dispatch_semaphore_create(1);
});
}
+ (NSString *)executeTask:(void (^)(void))task start:(NSTimeInterval)start interval:(NSTimeInterval)interval repeat:(BOOL)repeat async:(BOOL)async {
if (!task || start < 0 || (interval <= 0 && repeat)) return nil;
//获取队列
// dispatch_queue_t mainQueue = dispatch_get_main_queue();
dispatch_queue_t queue = async ? dispatch_queue_create("serialQueue", DISPATCH_QUEUE_SERIAL) : dispatch_get_main_queue();
//创建定时器
dispatch_source_t timer_t = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
//设置时间
dispatch_source_set_timer(timer_t, dispatch_time(DISPATCH_TIME_NOW, start * NSEC_PER_SEC), interval * NSEC_PER_SEC, 0);
//加锁
dispatch_semaphore_wait(semaphore_t_, DISPATCH_TIME_FOREVER);
//定时器唯一标识
NSString *taskSign = [NSString stringWithFormat:@"%lu", (unsigned long)timers_.count];
timers_[taskSign] = timer_t;
//解锁
dispatch_semaphore_signal(semaphore_t_);
//设置回调
//两个方法同时写,只会有一个执行,而且是最后那个
// dispatch_source_set_event_handler_f(timer_t, timerFunction);
dispatch_source_set_event_handler(timer_t, ^{
task();
if (!repeat) {
[self cancelTask:taskSign];
}
});
//启动定时
dispatch_resume(timer_t);
return taskSign;
}
+ (NSString *)executeTask:(id)target selector:(SEL)aSelector start:(NSTimeInterval)start interval:(NSTimeInterval)interval repeat:(BOOL)repeat async:(BOOL)async {
if (!target || !aSelector) return nil;
return [self executeTask:^{
if ([target respondsToSelector:aSelector]) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
[target performSelector:aSelector];
#pragma clang diagnostic pop
}
} start:start interval:interval repeat:repeat async:async];
}
+ (void)cancelTask:(NSString *)taskSign {
if (!taskSign.length) return;
//加锁
dispatch_semaphore_wait(semaphore_t_, DISPATCH_TIME_FOREVER);
dispatch_source_t timer_t = timers_[taskSign];
if (!timer_t) return;
dispatch_source_cancel(timers_[taskSign]);
[timers_ removeObjectForKey:taskSign];
//解锁
dispatch_semaphore_signal(semaphore_t_);
}
@end
使用示例
//
// ViewController.m
// 定时器-GCD
//
// Created by 宋法键 on 2019/8/14.
// Copyright © 2019 WZSG. All rights reserved.
//
#import "ViewController.h"
#import "SFJTimer.h"
@interface ViewController ()
@property (strong, nonatomic) dispatch_source_t timer_t;
@property (copy, nonatomic) NSString *taskSign1;
@property (copy, nonatomic) NSString *taskSign2;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_taskSign1 = [SFJTimer executeTask:^{
NSLog(@"-----aaaaa-----%@", [NSThread currentThread]);
} start:2.0f interval:1.0f repeat:NO async:NO];
_taskSign2 = [SFJTimer executeTask:self selector:@selector(doTask) start:2.0f interval:1.0f repeat:YES async:NO];
// self.timer_t = timer_t;
}
- (void)doTask {
NSLog(@"%s : %@", __func__, [NSThread currentThread]);
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[SFJTimer cancelTask:_taskSign1];
}
@end