2017年4月13日
一.效果如下
二.实现
#import <UIKit/UIKit.h>
@interface HuCountTimeView : UIView
@property (nonatomic, strong)UILabel *timeL;
//计时时间到后,回调接口
@property (nonatomic, copy) void (^timeOut)(void);
//到临界值修改颜色。
@property (nonatomic, copy) blk_t changeColorTimeOut;
/**
函数初始化函数
@param timeNum 倒计时最大值,需要转换成秒
@param dic 设置倒计时字体颜色等相关参数
@return 返回倒计时对象
*/
- (id)initWithTime:(NSInteger)timeNum AndDic:(NSDictionary*)dic;
//开始
- (void)startCountDown;
//暂停
- (void)stopCountDown;
//结束
- (void)endCountDown;
//定时器结束
- (void)countDownStop;
//当前耗时
- (NSInteger)getUseTime;
//剩余时间
- (NSInteger)getRemainTime;
@end
#import "HuCountTimeView.h"
@interface HuCountTimeView()
{
dispatch_source_t _timer;//定时器对象
NSInteger _timeNum;//初始化倒计时最大值
NSInteger _originalTimrNum; //出事后就不会变
}
@property (nonatomic, assign)BOOL isCreat;//首次创建Yes
@property (nonatomic, assign)BOOL isStart;//启动
@property (nonatomic, assign)BOOL isPause;//停止
@property (nonatomic, assign)BOOL needChangeColor;//是否需要改变
@property (nonatomic, assign)BOOL isSecondFlag;//是否只显示秒数
@property (nonatomic, assign)BOOL isDayFlag;//是否显示天数
@end
@implementation HuCountTimeView
- (id)initWithTime:(NSInteger)timeNum AndDic:(NSDictionary*)dic
{
self = [super init];
if (self) {
[self initDataWithTime:timeNum];
[self initViewWithDic:dic];
}
return self;
}
- (void)initDataWithTime:(NSInteger)timeNum
{
_originalTimrNum = _timeNum = timeNum;
}
- (void)initViewWithDic:(NSDictionary*)dic
{
self.timeL = [[UILabel alloc] initWithFrame:CGRectZero];
_timeL.font = dic[fontSizeKey] ?: fontsize_T3;
_timeL.textColor = dic[fontColorKey] ?: [UIColor blackColor];
_isSecondFlag = dic[isSecond];
_isDayFlag = dic[@"isDayFlag"];
[self addSubview:_timeL];
//首次设置frame
if (_isSecondFlag)
[self setCoutTimeView:[NSString stringWithFormat:@"%ldS",(long)_timeNum]];
else if(_isDayFlag)
[self setCoutTimeView:[HuConfigration timeDayStrWithNum:_timeNum]];
else
[self setCoutTimeView:[HuConfigration timeStrWithNum:_timeNum]];
}
- (void)setCoutTimeView:(NSString *)timeStr
{
_timeL.text = timeStr;
[self layoutView];
}
//开始
- (void)startCountDown
{
_isStart = YES;
[self createCountTime];
}
//暂停
- (void)stopCountDown
{
if (_isCreat) {
//挂起
dispatch_suspend(_timer);
_isPause = YES;
}
}
//结束
- (void)endCountDown
{
if (_isCreat) {
if(_isPause){
//恢复
dispatch_resume(_timer);
}else{
//移出
dispatch_source_cancel(_timer);
[self resetData];
}
}
}
- (void)countDownStop{
//移出
dispatch_source_cancel(_timer);
[self resetData];
}
- (void)createCountTime
{
if (_isCreat == NO) {
dispatch_queue_t globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
_timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, globalQueue);
_isCreat = YES;
}
//设置相关参数
//1.设置时间间隔,每秒执行一次
dispatch_source_set_timer(_timer, dispatch_walltime(NULL, 0), 1.0*NSEC_PER_SEC, 0);
//2.设置相关回调
WS(weakSelf);
dispatch_source_set_event_handler(_timer, ^{
NSString *strTime = [HuConfigration timeStrWithNum:_timeNum];
if (_isSecondFlag){
strTime = [NSString stringWithFormat:@"%ldS",(long)_timeNum];
}
if (_isDayFlag) {
strTime = [HuConfigration timeDayStrWithNum:_timeNum];
}
dispatch_async(dispatch_get_main_queue(), ^{
//在这根据自己的需求去刷新UI
[weakSelf setCoutTimeView:strTime];
});
//时间到了回调
if (_timeNum <= 0) {
if(_timeOut){
if(_timeOut){
_timeOut(); //时间到了回调
}
}
}else{
if(_timeNum <= 600)
{
if (_needChangeColor) {
if (_changeColorTimeOut) {
self.changeColorTimeOut();//修改颜色
_needChangeColor = NO;
}
}
}
_timeNum --;
}
});
//3.启动timer
dispatch_resume(_timer);
}
- (void)setChangeColorTimeOut:(blk_t)blk
{
if (_changeColorTimeOut == nil) {
_changeColorTimeOut = blk;
_needChangeColor = YES;
}
}
- (void)layoutView
{
UILabel *tmpLabel = self.timeL;
tmpLabel.textAlignment = NSTextAlignmentRight;
CGSize size = [tmpLabel.text sizeWithAttributes:@{NSFontAttributeName:tmpLabel.font}];
if (size.width < 28) {
size.width = 28;
}
CGRect frame = CGRectMake(0, 0, size.width, 25);
_timeL.frame = frame;
frame = self.frame;
frame.size = _timeL.frame.size;
self.frame = frame;
}
- (void)resetData
{
_isStart = NO;
_isCreat = NO;
_timeNum = 0;
NSString *defaultString = (_isSecondFlag) ? @"" : [HuConfigration timeStrWithNum:_timeNum];
if (_isDayFlag) {
defaultString = [HuConfigration timeDayStrWithNum:_timeNum];
}
[self setCoutTimeView:defaultString];
}
- (NSInteger)getUseTime
{
return _originalTimrNum - _timeNum;
}
- (NSInteger)getRemainTime
{
return _timeNum;
}
@end
如果您发现本文对你有所帮助,如果您认为其他人也可能受益,请把它分享出去。