iOS实现毫秒倒计时的方法详解
前言
大家应该都知道在app开发中,当展示限时优惠的某些商品时,往往会加一个倒计时,提示用户该商品限时优惠所剩的时间,。那对于开发者来说,这就需要我们去实现的是一个倒计时的功能,这个倒计时根据具体需求,可以以天、小时、分、秒、毫秒作单位。
今天呢,主要说说毫秒计时器。我们知道秒和毫秒之间的进制是1000,也就是说1秒=1000毫秒,那我们做毫秒倒计时器的时候是设置一个时间间隔为1毫秒的计时器,逐一减少毫秒数。但是这样的话太耗时了,所以很多的毫秒计时器中的毫秒数只是0-9之间的数字,这就意味着,这个毫秒计时器的时间间隔是100毫秒,这样相比起1毫秒为间隔的计时器,其消耗就少了很多,同时也达到毫秒计时的效果。
那对于整个毫秒倒计时的实现思路就是:得到未来某个日期的时间戳和当前日期的时间戳,计算这两者之间的时间差,然后设置一个时间间隔为100毫秒的计时器,每隔100毫秒,更新一下倒计时器上相应的数值。
实现方法
自定义一个UIview,将倒计时封装起来。
一、在MsecCountDownView.h中增加时间戳和计时器这两属性
@interfaceMsecCountDownView:UIView @property(nonatomic,assign)doubletimeInterval;//未来某个日期的时间戳 @property(nonatomic,strong)NSTimer*timer;//定时器 @end
二、在MsecCountDownView.m实现相关UI及倒计时方法
@interfaceMsecCountDownView(){
UIView*countdownBackView;
CGFloat_passTime;
}
@property(nonatomic,strong)UILabel*tipLabel;
@property(nonatomic,strong)UILabel*hoursLabel;
@property(nonatomic,strong)UILabel*minutesLabel;
@property(nonatomic,strong)UILabel*secondsLabel;
@property(nonatomic,strong)UILabel*millionSecondsLabel;
@property(nonatomic,strong)UILabel*label1;
@property(nonatomic,strong)UILabel*label2;
@property(nonatomic,strong)UILabel*label3;
@property(nonatomic,strong)UILabel*label4;
@end
创建相关UI
-(instancetype)initWithFrame:(CGRect)frame
{
self=[superinitWithFrame:frame];
if(self){
countdownBackView=[[UIViewalloc]initWithFrame:CGRectMake(0,0,self.frame.size.width,self.frame.size.height)];
[selfaddSubview:countdownBackView];
_tipLabel=[[UILabelalloc]init];
_tipLabel.frame=CGRectMake(0,0,40,countdownBackView.frame.size.height);
[countdownBackViewaddSubview:_tipLabel];
_tipLabel.font=[UIFontsystemFontOfSize:12];
//小时
_hoursLabel=[[UILabelalloc]initWithFrame:CGRectMake(_tipLabel.frame.origin.x+_tipLabel.frame.size.width,0,35,countdownBackView.frame.size.height)];
[countdownBackViewaddSubview:_hoursLabel];
_hoursLabel.font=[UIFontsystemFontOfSize:11];
_label1=[[UILabelalloc]initWithFrame:CGRectMake(_hoursLabel.frame.origin.x+_hoursLabel.frame.size.width,_hoursLabel.frame.origin.y,8,countdownBackView.frame.size.height)];
[countdownBackViewaddSubview:_label1];
//分钟
_minutesLabel=[[UILabelalloc]initWithFrame:CGRectMake(_label1.frame.origin.x+_label1.frame.size.width,_hoursLabel.frame.origin.y,20,countdownBackView.frame.size.height)];
[countdownBackViewaddSubview:_minutesLabel];
_minutesLabel.font=[UIFontsystemFontOfSize:11];
_label2=[[UILabelalloc]initWithFrame:CGRectMake(_minutesLabel.frame.origin.x+_minutesLabel.frame.size.width,_hoursLabel.frame.origin.y,8,countdownBackView.frame.size.height)];
[countdownBackViewaddSubview:_label2];
//秒
_secondsLabel=[[UILabelalloc]initWithFrame:CGRectMake(_label2.frame.origin.x+_label2.frame.size.width,_hoursLabel.frame.origin.y,20,countdownBackView.frame.size.height)];
[countdownBackViewaddSubview:_secondsLabel];
_secondsLabel.font=[UIFontsystemFontOfSize:11];
_label3=[[UILabelalloc]initWithFrame:CGRectMake(_secondsLabel.frame.origin.x+_secondsLabel.frame.size.width,_hoursLabel.frame.origin.y,8,countdownBackView.frame.size.height)];
[countdownBackViewaddSubview:_label3];
_millionSecondsLabel=[[UILabelalloc]initWithFrame:CGRectMake(_label3.frame.origin.x+_label3.frame.size.width,_hoursLabel.frame.origin.y,20,countdownBackView.frame.size.height)];
[countdownBackViewaddSubview:_millionSecondsLabel];
//毫秒
_millionSecondsLabel.font=[UIFontsystemFontOfSize:11];
_label1.textAlignment=1;
_label2.textAlignment=1;
_label3.textAlignment=1;
_hoursLabel.textAlignment=1;
_minutesLabel.textAlignment=1;
_secondsLabel.textAlignment=1;
_millionSecondsLabel.textAlignment=1;
_passTime=0.0;
}
returnself;
}
生成一个计时器
//得到未来某个日期的时间戳,与当前时间戳相比,得到两者的时间差,生成定时器
-(void)setTimeInterval:(double)timeInterval
{
_timeInterval=timeInterval;
NSDateFormatter*dataFormatter=[[NSDateFormatteralloc]init];
dataFormatter.dateFormat=@"MM/dd/yyyyHH:mm:ss.SSS";
//获取当前系统的时间,并用相应的格式转换
[dataFormatterstringFromDate:[NSDatedate]];
NSString*currentDayStr=[dataFormatterstringFromDate:[NSDatedate]];
NSDate*currentDate=[dataFormatterdateFromString:currentDayStr];
//优惠结束的时间,也用相同的格式去转换
NSDate*date=[NSDatedateWithTimeIntervalSince1970:timeInterval/1000.0];
NSString*deadlineStr=[dataFormatterstringFromDate:date];
NSDate*deadlineDate=[dataFormatterdateFromString:deadlineStr];
_timeInterval=[deadlineDatetimeIntervalSinceDate:currentDate]*1000;
if(_timeInterval!=0)
{
//时间间隔是100毫秒,也就是0.1秒
_timer=[NSTimerscheduledTimerWithTimeInterval:0.1ftarget:selfselector:@selector(timerAction)userInfo:nilrepeats:YES];
[[NSRunLoopcurrentRunLoop]addTimer:_timerforMode:UITrackingRunLoopMode];
}else{
[countdownBackViewremoveFromSuperview];
}
}
实现每隔100毫秒执行的方法,更新倒计时器上面相应的数值
//每间隔100毫秒定时器触发执行该方法
-(void)timerAction
{
[selfgetTimeFromTimeInterval:_timeInterval];
//当时间间隔为0时干掉定时器
if(_timeInterval-_passTime==0)
{
[_timerinvalidate];
_timer=nil;
}
}
//通过时间间隔计算具体时间(小时,分,秒,毫秒)
-(void)getTimeFromTimeInterval:(double)timeInterval
{
//1s=1000毫秒
_passTime+=100.f;//毫秒数从0-9,所以每次过去100毫秒
_tipLabel.text=@"还剩:";
_label3.text=@".";
_label2.text=@":";
_label1.text=@":";
//小时数
NSString*hours=[NSStringstringWithFormat:@"%ld",(NSInteger)((timeInterval-_passTime)/1000/60/60)];
//分钟数
NSString*minute=[NSStringstringWithFormat:@"%ld",(NSInteger)((timeInterval-_passTime)/1000/60)%60];
//秒数
NSString*second=[NSStringstringWithFormat:@"%ld",((NSInteger)(timeInterval-_passTime))/1000%60];
//毫秒数
CGFloatsss=((NSInteger)((timeInterval-_passTime)))%1000/100;
NSString*ss=[NSStringstringWithFormat:@"%.lf",sss];
if(minute.integerValue<10){
minute=[NSStringstringWithFormat:@"0%@",minute];
}
self.hoursLabel.text=[NSStringstringWithFormat:@"%@",hours];
self.minutesLabel.text=[NSStringstringWithFormat:@"%@",minute];
self.secondsLabel.text=[NSStringstringWithFormat:@"%@",second];
self.millionSecondsLabel.text=[NSStringstringWithFormat:@"%@",ss];
if(timeInterval-_passTime<=0){
[countdownBackViewremoveFromSuperview];
[selfremoveFromSuperview];
}
}
三、在ViewController.m给倒计时器赋值,实现自己想要的倒计时
-(void)viewDidLoad{
[superviewDidLoad];
msecView=[[MsecCountDownViewalloc]initWithFrame:CGRectMake(50,100,self.view.frame.size.width-100,16)];
[self.viewaddSubview:msecView];
NSDateFormatter*formatter=[[NSDateFormatteralloc]init];
[formattersetDateStyle:NSDateFormatterMediumStyle];
[formattersetTimeStyle:NSDateFormatterShortStyle];
[formattersetDateFormat:@"yyyy-MM-ddHH:mm:ss.SSS"];
NSDate*date=[formatterdateFromString:@"2017-04-1115:10:00.000"];
//将日期转换成时间戳
NSIntegertimeSp=[[NSNumbernumberWithDouble:[datetimeIntervalSince1970]]integerValue]*1000;
msecView.timeInterval=timeSp;
}
这样就实现倒计时的功能了。但是使用倒计时还需要注意一点,当离开该页面的时候,记得把定时器暂停,等回到该页面的时候再启动倒计时。
这个可以通过以下两方法实现。
-(void)viewWillAppear:(BOOL)animated{
//页面出现时,开启计时器
[msecView.timersetFireDate:[NSDatedistantPast]];
}
-(void)viewWillDisappear:(BOOL)animated{
//页面消失时,暂停提示器
[msecView.timersetFireDate:[NSDatedistantFuture]];
}
如有需要,可通过下面两种方法下载demo
一:GitHub上下载
二:本地下载
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对毛票票的支持。