引子
闲来无事,在各大论坛瞎逛的时候,发现了骑着jm的hi大神的《倒计时设计》一文,被深深的吸引。奈何水平有限,对C部分并不熟悉,所以借鉴大神的思路用OC写一个倒计时。思路
创建一个数组,数组中的元素是一个倒计时任务。在数组有值时,开启计时器,便利数组中的元素并根据条件进行block回调。数组为空时,停止计时器。数组中的元素为model类
// 倒计时精度
typedef enum : NSUInteger {
CountdownPrecisionDefault = 0, // 默认1秒为基础单位
CountdownPrecisionSmall, // 0.1秒为基础单位
} CountdownPrecision;
/**
倒计时回调
@param leftTime 剩余时间
@param isStop 为YES时,该任务结束
*/
typedef void(^CountdownBack)(long leftTime, BOOL * isStop);
@interface CountdownModel : NSObject
@property (nonatomic, assign) CountdownPrecision precision; // 精度
@property (nonatomic, strong) id receiver; // 注册对象
@property (nonatomic, assign) NSInteger allTime; // 总时长(单位:秒)
@property (nonatomic, assign) long leftTime; // 剩余时间(单位:毫秒)
@property (nonatomic, copy) CountdownBack block; // block回调
@end
- 创建倒计时单例类
/**
单例类初始化
@return 返回一个单例类
*/
+ (instancetype)shareManager;
- 添加任务
/**
注册一个倒计时
@param time 倒计时总时长
@param precision 精度
@param receiver 对象
@param block block回调
*/
- (void)registerCountdownRequiredTime:(NSInteger)time
precision:(CountdownPrecision)precision
Receiver:(id)receiver
block:(CountdownBack)block;
- 在单例类的.m中,创建几个属性
@interface CountdownManager ()
{
NSDate * _enterBackgroundTime; // app进入后台时的时间
}
@property (nonatomic, strong) NSTimer * timer; // 计时器
@property (nonatomic, strong) NSMutableArray * countdownArray; // 倒计时数组
@end
- 添加倒计时任务时,将计时器添加到NSRunLoop中,然后进行去重处理,最后将新任务添加到数组中
/**
注册一个倒计时
@param time 倒计时总时长
@param precision 精度
@param receiver 对象
@param block block回调
*/
- (void)registerCountdownRequiredTime:(NSInteger)time
precision:(CountdownPrecision)precision
Receiver:(id)receiver
block:(CountdownBack)block {
// 开启计时器
if (!_timer) [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
// 去重
for (CountdownModel * model in _countdownArray) {
if (model.receiver == receiver) return;
}
// 添加倒计时任务
CountdownModel * model = [CountdownModel new];
model.block = block;
model.allTime = time;
model.receiver = receiver;
model.precision = precision;
model.leftTime = time * 1000; // 毫秒
[self.countdownArray addObject:model];
}
- 计时器响应
- (void)timerAction {
// 没有任务时,移除计时器
if (_countdownArray.count == 0) {
[self endTimer];
return;
}
// 主线程回调block
dispatch_async(dispatch_get_main_queue(), ^{
for (NSInteger i = _countdownArray.count - 1; i >= 0; i --) {
CountdownModel * model = _countdownArray[i];
model.leftTime -= 100;
model.leftTime = model.leftTime < 0 ? 0 : model.leftTime;
[self callBackBlock:model];
}
});
}
- 任务回调判断
- (void)callBackBlock:(CountdownModel *)model {
// 是否停止该任务
BOOL isStop = NO;
// 每0.1秒进行一次回调
if (model.precision == CountdownPrecisionSmall) {
if (model.block) model.block(model.leftTime, &isStop);
}
// 每1秒进行一次回调
else if (model.precision == CountdownPrecisionDefault) {
// 剩余时间为整数时回调block
if (model.leftTime % 1000 == 0) {
if (model.block) model.block(model.leftTime, &isStop);
}
}
// 剩余时间为0时,移除该任务
if (model.leftTime <= 0) {
[_countdownArray removeObject:model];
}
// *isStop为YES时,停止该任务
if (isStop) [_countdownArray removeObject:model];
}
- app被挂起,记录下当前NSDate,在再次进入app时,计算时间差值,并进行block回调
#pragma mark app前台进入后台通知
- (void)enterBackgroundNotification {
_enterBackgroundTime = [NSDate date];
[self pauseTimer];
}
#pragma mark app后台进入前台通知
- (void)becomeActiveNotification {
// 后台被挂起的时间间隔
long dealy = [[NSDate date] timeIntervalSinceDate:_enterBackgroundTime];
// 主线程回调block
dispatch_async(dispatch_get_main_queue(), ^{
for (NSInteger i = _countdownArray.count - 1; i >= 0; i --) {
CountdownModel * model = _countdownArray[i];
model.leftTime -= dealy * 1000;
model.leftTime = model.leftTime < 0 ? 0 : model.leftTime;
[self callBackBlock:model];
}
});
[self resumeTiemr];
}