123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738 |
- //
- // YMTimer.m
- // yuemoClient
- //
- // Created by YoMi on 2023/11/6.
- //
- #import "YMTimer.h"
- #define YMPopupViewTimerPath(name) [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:[NSString stringWithFormat:@"YMTimer_%@_Timer",name]]
- @interface YMPopupViewTimerModel : NSObject
- /** 毫秒为单位计算 */
- @property (nonatomic, assign) NSTimeInterval time;
- /** 原始开始时间 毫秒 */
- @property (nonatomic, assign) NSTimeInterval originalTime;
- /** 进度单位 */
- @property (nonatomic, assign) NSTimeInterval unit;
- /** 是否本地持久化保存定时数据 */
- @property (nonatomic,assign) BOOL isDisk;
- /** 是否暂停 */
- @property (nonatomic,assign) BOOL isPause;
- /** 标识 */
- @property (nonatomic, copy) NSString *identifier;
- /** 通知名称 */
- @property (nonatomic, copy) NSString *NFName;
- /** 通知类型 0.不发通知 1.毫秒通知 2.秒通知 */
- @property (nonatomic, assign) YMTimerSecondChangeNFType NFType;
- /** 定时器更改block */
- @property (nonatomic, copy) YMTimerChangeBlock handleBlock;
- /** 定时器完成block */
- @property (nonatomic, copy) YMTimerFinishBlock finishBlock;
- /** 定时器暂停block */
- @property (nonatomic, copy) YMTimerPauseBlock pauseBlock;
- + (instancetype)timeInterval:(NSInteger)timeInterval;
- @end
- @implementation YMPopupViewTimerModel
- + (instancetype)timeInterval:(NSInteger)timeInterval {
- YMPopupViewTimerModel *object = [YMPopupViewTimerModel new];
- object.time = timeInterval*1000;
- object.originalTime = timeInterval*1000;
- return object;
- }
- - (void)encodeWithCoder:(NSCoder *)aCoder {
- [aCoder encodeDouble:self.time forKey:@"timeInterval"];
- [aCoder encodeDouble:self.originalTime forKey:@"oriTime"];
- [aCoder encodeDouble:self.unit forKey:@"unit"];
- [aCoder encodeBool:self.isDisk forKey:@"isDisk"];
- [aCoder encodeBool:self.isPause forKey:@"isPause"];
- [aCoder encodeObject:self.identifier forKey:@"identifier"];
- [aCoder encodeBool:self.NFType forKey:@"NFType"];
- }
- - (id)initWithCoder:(NSCoder *)aDecoder {
- self = [super init];
- if (self) {
- self.time = [aDecoder decodeDoubleForKey:@"timeInterval"];
- self.originalTime = [aDecoder decodeDoubleForKey:@"oriTime"];
- self.unit = [aDecoder decodeDoubleForKey:@"unit"];
- self.isDisk = [aDecoder decodeBoolForKey:@"isDisk"];
- self.isPause = [aDecoder decodeBoolForKey:@"isPause"];
- self.identifier = [aDecoder decodeObjectForKey:@"identifier"];
- self.NFType = [aDecoder decodeBoolForKey:@"NFType"];
- }
- return self;
- }
- @end
- @interface YMTimer ()
- @property (nonatomic, strong) NSTimer * _Nullable showTimer;
- /** 储存多个计时器数据源 */
- @property (nonatomic, strong) NSMutableDictionary<NSString *, YMPopupViewTimerModel *> *timerMdic;
- @end
- @implementation YMTimer
- static YMTimer *_instance;
- YMTimer *YMTimerM() {
- return [YMTimer sharedInstance];
- }
- + (instancetype)sharedInstance {
- if (!_instance) {
- static dispatch_once_t onceToken;
- dispatch_once(&onceToken, ^{
- _instance = [[self alloc] init];
- });
- }
- return _instance;
- }
- + (id)allocWithZone:(struct _NSZone *)zone {
- static dispatch_once_t onceToken;
- dispatch_once(&onceToken, ^{
- _instance = [super allocWithZone:zone];
- });
- return _instance;
- }
- /// 设置倒计时任务的通知回调
- /// @param name 通知名
- /// @param identifier 倒计时任务的标识
- /// @param type 倒计时变化通知类型
- + (void)setNotificationForName:(NSString *)name identifier:(NSString *)identifier changeNFType:(YMTimerSecondChangeNFType)type {
- if (identifier.length<=0) {
- NSLog(@"计时器标识不能为空");
- return;
- }
- YMPopupViewTimerModel *model = YMTimerM().timerMdic[identifier];
- if (model) {
- model.NFType = type;
- model.NFName = name;
- return;
- }else {
- NSLog(@"找不到计时器任务");
- return;
- }
- }
- /** 添加定时器并开启计时 */
- + (void)addTimerForTime:(NSTimeInterval)time handle:(YMTimerChangeBlock)handle {
- [self initTimerForForTime:time identifier:nil ForIsDisk:NO unit:0 handle:handle finish:nil pause:nil];
- }
- /** 添加定时器并开启计时 */
- + (void)addTimerForTime:(NSTimeInterval)time
- identifier:(NSString *)identifier
- handle:(YMTimerChangeBlock)handle {
- [self initTimerForForTime:time identifier:identifier ForIsDisk:NO unit:-1 handle:handle finish:nil pause:nil];
- }
- /** 添加定时器并开启计时 */
- + (void)addTimerForTime:(NSTimeInterval)time
- identifier:(NSString *)identifier
- handle:(YMTimerChangeBlock)handle
- finish:(YMTimerFinishBlock)finishBlock
- pause:(YMTimerPauseBlock)pauseBlock {
- [self initTimerForForTime:time identifier:identifier ForIsDisk:NO unit:-1 handle:handle finish:finishBlock pause:finishBlock];
- }
- /** 添加定时器并开启计时 */
- + (void)addDiskTimerForTime:(NSTimeInterval)time
- identifier:(NSString *)identifier
- handle:(YMTimerChangeBlock)handle {
- [self initTimerForForTime:time identifier:identifier ForIsDisk:YES unit:-1 handle:handle finish:nil pause:nil];
- }
- /** 添加定时器并开启计时 */
- + (void)addDiskTimerForTime:(NSTimeInterval)time
- identifier:(NSString *)identifier
- handle:(YMTimerChangeBlock)handle
- finish:(YMTimerFinishBlock)finishBlock
- pause:(YMTimerPauseBlock)pauseBlock {
- [self initTimerForForTime:time identifier:identifier ForIsDisk:YES unit:-1 handle:handle finish:finishBlock pause:pauseBlock];
- }
- /** 添加定时器并开启计时 */
- + (void)addMinuteTimerForTime:(NSTimeInterval)time handle:(YMTimerChangeBlock)handle {
- [self initTimerForForTime:time identifier:nil ForIsDisk:NO unit:1000 handle:handle finish:nil pause:nil];
- }
- /** 添加定时器并开启计时 */
- + (void)addMinuteTimerForTime:(NSTimeInterval)time
- identifier:(NSString *)identifier
- handle:(YMTimerChangeBlock)handle {
- [self initTimerForForTime:time identifier:identifier ForIsDisk:NO unit:1000 handle:handle finish:nil pause:nil];
- }
- /** 添加定时器并开启计时 */
- + (void)addMinuteTimerForTime:(NSTimeInterval)time
- identifier:(NSString *)identifier
- handle:(YMTimerChangeBlock)handle
- finish:(YMTimerFinishBlock)finishBlock
- pause:(YMTimerPauseBlock)pauseBlock {
- [self initTimerForForTime:time identifier:identifier ForIsDisk:NO unit:1000 handle:handle finish:finishBlock pause:finishBlock];
- }
- /** 添加定时器并开启计时 */
- + (void)addDiskMinuteTimerForTime:(NSTimeInterval)time
- identifier:(NSString *)identifier
- handle:(YMTimerChangeBlock)handle
- finish:(YMTimerFinishBlock)finishBlock
- pause:(YMTimerPauseBlock)pauseBlock {
- [self initTimerForForTime:time identifier:identifier ForIsDisk:YES unit:1000 handle:handle finish:finishBlock pause:pauseBlock];
- }
- //总初始化入口
- + (void)initTimerForForTime:(NSTimeInterval)time
- identifier:(NSString *)identifier
- ForIsDisk:(BOOL)isDisk
- unit:(NSTimeInterval)unit
- handle:(YMTimerChangeBlock)handle
- finish:(YMTimerFinishBlock)finishBlock
- pause:(YMTimerPauseBlock)pauseBlock {
-
- if (identifier.length<=0) {
- YMPopupViewTimerModel *model = [YMPopupViewTimerModel timeInterval:time];
- model.isDisk = isDisk;
- model.identifier = [NSString stringWithFormat:@"%p",model];
- model.unit = unit;
- model.handleBlock = handle;
- model.finishBlock = finishBlock;
- model.pauseBlock = pauseBlock;
- [YMTimerM().timerMdic setObject:model forKey:model.identifier];
- if (model.handleBlock) {
- NSInteger totalSeconds = model.time/1000.0;
- NSString *days = [NSString stringWithFormat:@"%zd", totalSeconds/60/60/24];
- NSString *hours = [NSString stringWithFormat:@"%zd", totalSeconds/60/60%24];
- NSString *minute = [NSString stringWithFormat:@"%zd", (totalSeconds/60)%60];
- NSString *second = [NSString stringWithFormat:@"%zd", totalSeconds%60];
- CGFloat sss = ((NSInteger)(model.time))%1000/10;
- NSString *ss = [NSString stringWithFormat:@"%.lf", sss];
-
- if (hours.integerValue < 10) {
- hours = [NSString stringWithFormat:@"0%@", hours];
- }
- if (minute.integerValue < 10) {
- minute = [NSString stringWithFormat:@"0%@", minute];
- }
- if (second.integerValue < 10) {
- second = [NSString stringWithFormat:@"0%@", second];
- }
- if (ss.integerValue < 10) {
- ss = [NSString stringWithFormat:@"0%@", ss];
- }
- model.handleBlock(days,hours,minute,second,ss);
-
- }
- // 发出通知
- if (model.NFType != YMTimerSecondChangeNFTypeNone) {
- [[NSNotificationCenter defaultCenter] postNotificationName:model.NFName object:nil userInfo:nil];
- }
- if (model.isDisk) {
- [self savaForTimerModel:model];
- }
- [self initTimer];
- return;
- }
-
-
- BOOL isTempDisk = [YMTimer timerIsExistInDiskForIdentifier:identifier];//磁盘有任务
- BOOL isRAM = YMTimerM().timerMdic[identifier]?YES:NO;//内存有任务
-
- if (!isRAM && !isTempDisk) {//新任务
- YMPopupViewTimerModel *model = [YMPopupViewTimerModel timeInterval:time];
- model.handleBlock = handle;
- model.isDisk = isDisk;
- model.identifier = identifier;
- model.unit = unit;
- model.finishBlock = finishBlock;
- model.pauseBlock = pauseBlock;
- [YMTimerM().timerMdic setObject:model forKey:identifier];
- if (model.handleBlock) {
-
- NSInteger totalSeconds = model.time/1000.0;
- NSString *days = [NSString stringWithFormat:@"%zd", totalSeconds/60/60/24];
- NSString *hours = [NSString stringWithFormat:@"%zd", totalSeconds/60/60%24];
- NSString *minute = [NSString stringWithFormat:@"%zd", (totalSeconds/60)%60];
- NSString *second = [NSString stringWithFormat:@"%zd", totalSeconds%60];
- CGFloat sss = ((NSInteger)(model.time))%1000/10;
- NSString *ss = [NSString stringWithFormat:@"%.lf", sss];
-
- if (hours.integerValue < 10) {
- hours = [NSString stringWithFormat:@"0%@", hours];
- }
- if (minute.integerValue < 10) {
- minute = [NSString stringWithFormat:@"0%@", minute];
- }
- if (second.integerValue < 10) {
- second = [NSString stringWithFormat:@"0%@", second];
- }
- if (ss.integerValue < 10) {
- ss = [NSString stringWithFormat:@"0%@", ss];
- }
- if (model.isDisk) {
- [self savaForTimerModel:model];
- }
- model.handleBlock(days,hours,minute,second,ss);
-
- }
- // 发出通知
- if (model.NFType != YMTimerSecondChangeNFTypeNone) {
- [[NSNotificationCenter defaultCenter] postNotificationName:model.NFName object:nil userInfo:nil];
- }
- [self initTimer];
- }
-
-
- if (isRAM && !isTempDisk) {//内存任务
- YMPopupViewTimerModel *model = YMTimerM().timerMdic[identifier];
- model.isPause = NO;
- model.handleBlock = handle;
- model.isDisk = isDisk;
- model.finishBlock = finishBlock;
- model.pauseBlock = pauseBlock;
- if (model.isDisk) {
- [self savaForTimerModel:model];
- }
- // [self initTimer];
- }
-
- if (!isRAM && isTempDisk) {//硬盘的任务
- YMPopupViewTimerModel *model = [YMTimer getTimerModelForIdentifier:identifier];
- if (isDisk == NO) {
- [YMTimer deleteForIdentifier:identifier];
- }
- model.isPause = NO;
- model.isDisk = isDisk;
- model.handleBlock = handle;
- model.finishBlock = finishBlock;
- model.pauseBlock = pauseBlock;
- [YMTimerM().timerMdic setObject:model forKey:identifier];
- if (model.handleBlock) {
- NSInteger totalSeconds = model.time/1000.0;
- NSString *days = [NSString stringWithFormat:@"%zd", totalSeconds/60/60/24];
- NSString *hours = [NSString stringWithFormat:@"%zd", totalSeconds/60/60%24];
- NSString *minute = [NSString stringWithFormat:@"%zd", (totalSeconds/60)%60];
- NSString *second = [NSString stringWithFormat:@"%zd", totalSeconds%60];
- CGFloat sss = ((NSInteger)(model.time))%1000/10;
- NSString *ss = [NSString stringWithFormat:@"%.lf", sss];
-
- if (hours.integerValue < 10) {
- hours = [NSString stringWithFormat:@"0%@", hours];
- }
- if (minute.integerValue < 10) {
- minute = [NSString stringWithFormat:@"0%@", minute];
- }
- if (second.integerValue < 10) {
- second = [NSString stringWithFormat:@"0%@", second];
- }
- if (ss.integerValue < 10) {
- ss = [NSString stringWithFormat:@"0%@", ss];
- }
- model.handleBlock(days,hours,minute,second,ss);
-
- }
- // 发出通知
- if (model.NFType != YMTimerSecondChangeNFTypeNone) {
- [[NSNotificationCenter defaultCenter] postNotificationName:model.NFName object:nil userInfo:nil];
- }
- if (model.isDisk) {
- [self savaForTimerModel:model];
- }
- [self initTimer];
- }
-
- if (isRAM && isTempDisk) {//硬盘的任务
- YMPopupViewTimerModel *model = [YMTimer getTimerModelForIdentifier:identifier];
- model.isPause = NO;
- if (isDisk == NO) {
- [YMTimer deleteForIdentifier:identifier];
- }
- model.isDisk = isDisk;
- model.handleBlock = handle;
- model.finishBlock = finishBlock;
- model.pauseBlock = pauseBlock;
- [YMTimerM().timerMdic setObject:model forKey:identifier];
- if (model.handleBlock) {
- NSInteger totalSeconds = model.time/1000.0;
- NSString *days = [NSString stringWithFormat:@"%zd", totalSeconds/60/60/24];
- NSString *hours = [NSString stringWithFormat:@"%zd", totalSeconds/60/60%24];
- NSString *minute = [NSString stringWithFormat:@"%zd", (totalSeconds/60)%60];
- NSString *second = [NSString stringWithFormat:@"%zd", totalSeconds%60];
- CGFloat sss = ((NSInteger)(model.time))%1000/10;
- NSString *ss = [NSString stringWithFormat:@"%.lf", sss];
-
- if (hours.integerValue < 10) {
- hours = [NSString stringWithFormat:@"0%@", hours];
- }
- if (minute.integerValue < 10) {
- minute = [NSString stringWithFormat:@"0%@", minute];
- }
- if (second.integerValue < 10) {
- second = [NSString stringWithFormat:@"0%@", second];
- }
- if (ss.integerValue < 10) {
- ss = [NSString stringWithFormat:@"0%@", ss];
- }
- model.handleBlock(days,hours,minute,second,ss);
-
- }
- // 发出通知
- if (model.NFType != YMTimerSecondChangeNFTypeNone) {
- [[NSNotificationCenter defaultCenter] postNotificationName:model.NFName object:nil userInfo:nil];
- }
- if (model.isDisk) {
- [self savaForTimerModel:model];
- }
- // [self initTimer];
- }
-
- }
- + (NSTimeInterval)getTimeIntervalForIdentifier:(NSString *)identifier {
- if (identifier.length<=0) {
- return 0.0;
- }
-
- BOOL isTempDisk = [YMTimer timerIsExistInDiskForIdentifier:identifier];//磁盘有任务
- BOOL isRAM = YMTimerM().timerMdic[identifier]?YES:NO;//内存有任务
-
-
- if (isTempDisk) {
- YMPopupViewTimerModel *model = [YMTimer loadTimerForIdentifier:identifier];
- return model.originalTime - model.time;
- }else if (isRAM) {
- YMPopupViewTimerModel *model = YMTimerM().timerMdic[identifier];
- return model.originalTime - model.time;
- }else {
- NSLog(@"找不到计时任务");
- return 0.0;
- }
-
- }
- + (BOOL)pauseTimerForIdentifier:(NSString *)identifier {
- if (identifier.length<=0) {
- NSLog(@"计时器标识不能为空");
- return NO;
- }
- YMPopupViewTimerModel *model = YMTimerM().timerMdic[identifier];
-
- if (model) {
- model.isPause = YES;
- if (model.pauseBlock) { model.pauseBlock(model.identifier); }
- return YES;
- }else {
- NSLog(@"找不到计时器任务");
- return NO;
- }
- }
- + (void)pauseAllTimer {
- [YMTimerM().timerMdic enumerateKeysAndObjectsUsingBlock:^(NSString *key, YMPopupViewTimerModel *obj, BOOL *stop) {
- obj.isPause = YES;
- if (obj.pauseBlock) { obj.pauseBlock(obj.identifier); }
- }];
- }
- + (BOOL)restartTimerForIdentifier:(NSString *)identifier {
- if (identifier.length<=0) {
- NSLog(@"计时器标识不能为空");
- return NO;
- }
-
- //只有内存任务才能重启, 硬盘任务只能调用addTimer系列方法重启
- BOOL isRAM = YMTimerM().timerMdic[identifier]?YES:NO;//内存有任务
- if (isRAM) {
- YMPopupViewTimerModel *model = YMTimerM().timerMdic[identifier];
- model.isPause = NO;
- return YES;
- }else {
- NSLog(@"找不到计时器任务");
- return NO;
- }
-
-
- }
- + (void)restartAllTimer {
-
- if (YMTimerM().timerMdic.count<=0) {
- return;
- }
-
- [YMTimerM().timerMdic enumerateKeysAndObjectsUsingBlock:^(NSString *key, YMPopupViewTimerModel *obj, BOOL *stop) {
- obj.isPause = NO;
- }];
- }
- + (BOOL)resetTimerForIdentifier:(NSString *)identifier {
- if (identifier.length<=0) {
- NSLog(@"计时器标识不能为空");
- return NO;
- }
-
- //只有内存任务才能重启, 硬盘任务只能调用addTimer系列方法重启
- BOOL isRAM = YMTimerM().timerMdic[identifier]?YES:NO;//内存有任务
- if (isRAM) {
- YMPopupViewTimerModel *model = YMTimerM().timerMdic[identifier];
- model.isPause = NO;
- model.time = model.originalTime;
- if (model.handleBlock) {
- NSInteger totalSeconds = model.time/1000.0;
- NSString *days = [NSString stringWithFormat:@"%zd", totalSeconds/60/60/24];
- NSString *hours = [NSString stringWithFormat:@"%zd", totalSeconds/60/60%24];
- NSString *minute = [NSString stringWithFormat:@"%zd", (totalSeconds/60)%60];
- NSString *second = [NSString stringWithFormat:@"%zd", totalSeconds%60];
- CGFloat sss = ((NSInteger)(model.time))%1000/10;
- NSString *ss = [NSString stringWithFormat:@"%.lf", sss];
-
- if (hours.integerValue < 10) {
- hours = [NSString stringWithFormat:@"0%@", hours];
- }
- if (minute.integerValue < 10) {
- minute = [NSString stringWithFormat:@"0%@", minute];
- }
- if (second.integerValue < 10) {
- second = [NSString stringWithFormat:@"0%@", second];
- }
- if (ss.integerValue < 10) {
- ss = [NSString stringWithFormat:@"0%@", ss];
- }
- model.handleBlock(days,hours,minute,second,ss);
-
- }
- return YES;
- }else {
- NSLog(@"找不到计时器任务");
- return NO;
- }
- }
- + (void)resetAllTimer {
- if (YMTimerM().timerMdic.count<=0) {
- return;
- }
-
- [YMTimerM().timerMdic enumerateKeysAndObjectsUsingBlock:^(NSString *key, YMPopupViewTimerModel *obj, BOOL *stop) {
- obj.isPause = NO;
- obj.time = obj.originalTime;
- if (obj.handleBlock) {
- NSInteger totalSeconds = obj.time/1000.0;
- NSString *days = [NSString stringWithFormat:@"%zd", totalSeconds/60/60/24];
- NSString *hours = [NSString stringWithFormat:@"%zd", totalSeconds/60/60%24];
- NSString *minute = [NSString stringWithFormat:@"%zd", (totalSeconds/60)%60];
- NSString *second = [NSString stringWithFormat:@"%zd", totalSeconds%60];
- CGFloat sss = ((NSInteger)(obj.time))%1000/10;
- NSString *ss = [NSString stringWithFormat:@"%.lf", sss];
-
- if (hours.integerValue < 10) {
- hours = [NSString stringWithFormat:@"0%@", hours];
- }
- if (minute.integerValue < 10) {
- minute = [NSString stringWithFormat:@"0%@", minute];
- }
- if (second.integerValue < 10) {
- second = [NSString stringWithFormat:@"0%@", second];
- }
- if (ss.integerValue < 10) {
- ss = [NSString stringWithFormat:@"0%@", ss];
- }
- obj.handleBlock(days,hours,minute,second,ss);
-
- }
- }];
- }
- + (BOOL)removeTimerForIdentifier:(NSString *)identifier {
- if (identifier.length<=0) {
- NSLog(@"计时器标识不能为空");
- return NO;
- }
-
- [YMTimerM().timerMdic removeObjectForKey:identifier];
- if (YMTimerM().timerMdic.count<=0) {//如果没有计时任务了 就销毁计时器
- [YMTimerM().showTimer invalidate];
- YMTimerM().showTimer = nil;
- }
- return YES;
- }
- + (void)removeAllTimer {
- [YMTimerM().timerMdic removeAllObjects];
- [YMTimerM().showTimer invalidate];
- YMTimerM().showTimer = nil;
- }
- /** increase YES: 递增 NO: 递减 */
- + (void)initTimer {
-
- if (YMTimerM().showTimer) {
- return;
- }
-
- NSTimer *timer = [NSTimer timerWithTimeInterval:0.01f target:YMTimerM() selector:@selector(timerChange) userInfo:nil repeats:YES];
- [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
- YMTimerM().showTimer = timer;
-
- }
- - (void)timerChange {
- // 时间差+
- [YMTimerM().timerMdic enumerateKeysAndObjectsUsingBlock:^(NSString *key, YMPopupViewTimerModel *obj, BOOL *stop) {
- if (!obj.isPause) {
-
- obj.time = obj.time-10.0;
-
- if (obj.unit>-1) {
- obj.unit = obj.unit-10.0;
- }
-
- if (obj.time<0) {//计时结束
- obj.time = 0;
- obj.isPause = YES;
- }
- NSInteger totalSeconds = obj.time/1000.0;
- NSString *days = [NSString stringWithFormat:@"%zd", totalSeconds/60/60/24];
- NSString *hours = [NSString stringWithFormat:@"%zd", totalSeconds/60/60%24];
- NSString *minute = [NSString stringWithFormat:@"%zd", (totalSeconds/60)%60];
- NSString *second = [NSString stringWithFormat:@"%zd", totalSeconds%60];
- CGFloat sss = ((NSInteger)(obj.time))%1000/10;
- NSString *ms = [NSString stringWithFormat:@"%.lf", sss];
-
- if (hours.integerValue < 10) {
- hours = [NSString stringWithFormat:@"0%@", hours];
- }
- if (minute.integerValue < 10) {
- minute = [NSString stringWithFormat:@"0%@", minute];
- }
- if (second.integerValue < 10) {
- second = [NSString stringWithFormat:@"0%@", second];
- }
- if (ms.integerValue < 10) {
- ms = [NSString stringWithFormat:@"0%@", ms];
- }
-
-
-
- if (obj.unit<=-1) {
- if (obj.handleBlock) {obj.handleBlock(days,hours,minute,second,ms);}
-
- if (obj.NFType == YMTimerSecondChangeNFTypeMilliSecond) {
- // 发出通知
- [[NSNotificationCenter defaultCenter] postNotificationName:obj.NFName object:nil userInfo:nil];
- }
- }else if (obj.unit == 0) {
- if (obj.handleBlock) {obj.handleBlock(days,hours,minute,second,ms);}
- obj.unit = 1000;
- if (obj.NFType == YMTimerSecondChangeNFTypeSecond) {
- // 发出通知
- [[NSNotificationCenter defaultCenter] postNotificationName:obj.NFName object:nil userInfo:nil];
- }
- }
-
- if (obj.isDisk) {
- [YMTimer savaForTimerModel:obj];
- }
-
- if (obj.time<=0) {//计时器计时完毕自动移除计时任务
- if (obj.finishBlock) { obj.finishBlock(obj.identifier); }
- [YMTimerM().timerMdic removeObjectForKey:obj.identifier];
- [YMTimer deleteForIdentifier:obj.identifier];
- }
-
- }
- }];
-
- }
- - (NSMutableDictionary<NSString *,YMPopupViewTimerModel *> *)timerMdic {
- if(_timerMdic) return _timerMdic;
- _timerMdic = [NSMutableDictionary dictionary];
- return _timerMdic;
- }
- #pragma mark - ***** other *****
- + (BOOL)timerIsExistInDiskForIdentifier:(NSString *)identifier {
- NSString *filePath = YMPopupViewTimerPath(identifier);
- BOOL isExist = [[NSFileManager defaultManager] fileExistsAtPath:filePath];
- return isExist;
- }
- /** 格式化时间 */
- + (void)formatDateForTime:(NSTimeInterval)time handle:(YMTimerChangeBlock)handle {
- if (handle) {
- NSInteger totalSeconds = time/1000.0;
- NSString *days = [NSString stringWithFormat:@"%zd", totalSeconds/60/60/24];
- NSString *hours = [NSString stringWithFormat:@"%zd", totalSeconds/60/60%24];
- NSString *minute = [NSString stringWithFormat:@"%zd", (totalSeconds/60)%60];
- NSString *second = [NSString stringWithFormat:@"%zd", totalSeconds%60];
- CGFloat sss = ((NSInteger)(time))%1000/10;
- NSString *ms = [NSString stringWithFormat:@"%.lf", sss];
-
- if (hours.integerValue < 10) {
- hours = [NSString stringWithFormat:@"0%@", hours];
- }
- if (minute.integerValue < 10) {
- minute = [NSString stringWithFormat:@"0%@", minute];
- }
- if (second.integerValue < 10) {
- second = [NSString stringWithFormat:@"0%@", second];
- }
- if (ms.integerValue < 10) {
- ms = [NSString stringWithFormat:@"0%@", ms];
- }
-
- handle(days,hours,minute,second,ms);
- }
- }
- + (BOOL)savaForTimerModel:(YMPopupViewTimerModel *)model {
- NSString *filePath = YMPopupViewTimerPath(model.identifier);
- return [NSKeyedArchiver archiveRootObject:model toFile:filePath];
- }
- + (YMPopupViewTimerModel *)loadTimerForIdentifier:(NSString *)identifier{
- NSString *filePath = YMPopupViewTimerPath(identifier);
- return [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
- }
- + (BOOL)deleteForIdentifier:(NSString *)identifier {
- NSString *filePath = YMPopupViewTimerPath(identifier);
- NSFileManager* fileManager = [NSFileManager defaultManager];
- BOOL isExist = [fileManager fileExistsAtPath:filePath];
- if (isExist) {
- return [fileManager removeItemAtPath:filePath error:nil];
- }
- return NO;
- }
- + (YMPopupViewTimerModel *)getTimerModelForIdentifier:(NSString *)identifier {
- if (identifier.length<=0) {
- return nil;
- }
- YMPopupViewTimerModel *model = [YMTimer loadTimerForIdentifier:identifier];
- return model;
-
- }
- @end
|