簡單介紹
1瞧壮、單例類管理GCD計時器;
2匙握、類方法調用計時器API咆槽,方便快捷;
3圈纺、提供三組API秦忿,快速實現(xiàn)計時麦射、倒計時、取消計時灯谣;
4违寞、目前僅提供同時存在1組計時器刚盈;
5男杈、計時結束后栏妖,自動取消,也可手動取消計時器呐萨;
注意事項
1杀饵、在頁面加載和退出時,注意定時器的銷毀谬擦。
2切距、及時調用銷毀方法,避免釋放不掉的問題惨远。
例如:
+ (void)cancelSingleTimer;
.h
構建回調block
/*
每秒頻單頻計時器回調
current 每秒回調一次 回調當前的時間
end 當為TRUE時 定時器結束
*/
typedef void (^DKTimerSingleTimerBlock)(NSInteger current, BOOL end);
/*
DKTimer
*/
@interface DKTimer : NSObject
/*
倒計時
time:從xx秒開始倒計時
block: 回調
*/
+ (void)countdown:(NSInteger)time block:(DKTimerSingleTimerBlock)block;
/*
計時
max:計時xx時間
block: 回調
*/
+ (void)singleTimer:(NSInteger)max block:(DKTimerSingleTimerBlock)block;
/*
取消計時
*/
+ (void)cancelSingleTimer;
.m
聲明屬性
/*
每秒頻單頻計時器谜悟;
每次調用前,會銷毀之前的定時器北秽;
支持計時和倒計時葡幸。倒計時到0時,停止計時贺氓;
回調的block均為主線程蔚叨;
*/
@property (strong, nonatomic) dispatch_source_t singleTimer;
@property (assign, nonatomic) NSInteger current;
@property (assign, nonatomic) NSInteger max;
@property (assign, nonatomic) BOOL sequence;
@property (copy, nonatomic) DKTimerSingleTimerBlock singleTimerBlock;
構建單例類
/*
單例類的創(chuàng)建
使用單例類創(chuàng)建定時器,在一定的環(huán)境下辙培,可以高效的控制定時器蔑水。
*/
+ (DKTimer *)timer {
static dispatch_once_t onceToken;
static DKTimer *timer = nil;
dispatch_once(&onceToken, ^{
timer = [[DKTimer alloc] init];
});
return timer;
}
公有API
/*
倒計時 最小從1開始倒計時
time:從xx秒開始倒計時
block: 回調
*/
+ (void)countdown:(NSInteger)time block:(DKTimerSingleTimerBlock)block {
if (time <= 0) {
return;
}
[DKTimer timer].singleTimerBlock = block;
[DKTimer timer].sequence = NO;
[DKTimer timer].current = time;
[[DKTimer timer] startSingleTime];
}
/*
計時
max:計時xx時間
block: 回調
*/
+ (void)singleTimer:(NSInteger)max block:(DKTimerSingleTimerBlock)block {
[DKTimer timer].singleTimerBlock = block;
[DKTimer timer].sequence = YES;
[DKTimer timer].max = max;
[DKTimer timer].current = 0;
[[DKTimer timer] startSingleTime];
}
/*
取消計時
*/
+ (void)cancelSingleTimer {
[[DKTimer timer] stopSingleTimer];
}
私有API
/*
計時器的回調方法 主線程
*/
- (void)singleTimerMethod {
if ([DKTimer timer].sequence) {
// 計時
[DKTimer timer].current = [DKTimer timer].current + 1;
BOOL end;
if ([DKTimer timer].current > [DKTimer timer].max) {
// 結束
end = YES;
[self stopSingleTimer];
}else {
// 繼續(xù)
end = NO;
}
if ([DKTimer timer].singleTimerBlock) {
[DKTimer timer].singleTimerBlock([DKTimer timer].current, end);
}
}else {
// 倒計時
[DKTimer timer].current = [DKTimer timer].current - 1;
BOOL end;
if ([DKTimer timer].current < 0) {
// 結束
end = YES;
[self stopSingleTimer];
}else {
// 繼續(xù)
end = NO;
}
if ([DKTimer timer].singleTimerBlock) {
[DKTimer timer].singleTimerBlock([DKTimer timer].current, end);
}
}
}
計時器創(chuàng)建 開始 銷毀
- (void)stopSingleTimer {
if (_singleTimer) {
dispatch_cancel(_singleTimer);
_singleTimer = nil;
}
}
- (void)startSingleTime {
[self stopSingleTimer];
__weak DKTimer *weakself = self;
NSInteger timeSpace = 1;
dispatch_queue_t queue = dispatch_get_main_queue();
self.singleTimer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
dispatch_time_t start = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC));
uint64_t interval = (uint64_t)(timeSpace * NSEC_PER_SEC);
dispatch_source_set_timer(self.singleTimer, start, interval, 0);
dispatch_source_set_event_handler(self.singleTimer, ^{
dispatch_async(dispatch_get_main_queue(), ^{
[weakself singleTimerMethod];
});
});
dispatch_resume(self.singleTimer);
}