iOS如何实现毫秒倒计时-创新互联
这篇文章将为大家详细讲解有关iOS如何实现毫秒倒计时,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
为连云港等地区用户提供了全套网页设计制作服务,及连云港网站建设行业解决方案。主营业务为成都网站建设、做网站、连云港网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!实现方法
自定义一个UIview,将倒计时封装起来。
一、在MsecCountDownView.h中增加时间戳和计时器这两属性
@interface MsecCountDownView : UIView @property(nonatomic, assign)double timeInterval;//未来某个日期的时间戳 @property(nonatomic, strong)NSTimer *timer ; //定时器 @end
二、在MsecCountDownView.m实现相关UI及倒计时方法
@interface MsecCountDownView (){ 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 = [super initWithFrame:frame]; if (self) { countdownBackView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)]; [self addSubview:countdownBackView]; _tipLabel=[[UILabel alloc] init]; _tipLabel.frame = CGRectMake(0, 0, 40, countdownBackView.frame.size.height); [countdownBackView addSubview:_tipLabel]; _tipLabel.font = [UIFont systemFontOfSize:12]; //小时 _hoursLabel=[[UILabel alloc] initWithFrame:CGRectMake(_tipLabel.frame.origin.x+_tipLabel.frame.size.width, 0, 35, countdownBackView.frame.size.height)]; [countdownBackView addSubview:_hoursLabel]; _hoursLabel.font = [UIFont systemFontOfSize:11]; _label1=[[UILabel alloc] initWithFrame:CGRectMake(_hoursLabel.frame.origin.x+_hoursLabel.frame.size.width, _hoursLabel.frame.origin.y, 8, countdownBackView.frame.size.height)]; [countdownBackView addSubview:_label1]; //分钟 _minutesLabel=[[UILabel alloc] initWithFrame:CGRectMake(_label1.frame.origin.x+_label1.frame.size.width, _hoursLabel.frame.origin.y, 20, countdownBackView.frame.size.height)]; [countdownBackView addSubview:_minutesLabel]; _minutesLabel.font = [UIFont systemFontOfSize:11]; _label2=[[UILabel alloc] initWithFrame:CGRectMake(_minutesLabel.frame.origin.x+_minutesLabel.frame.size.width, _hoursLabel.frame.origin.y, 8, countdownBackView.frame.size.height)]; [countdownBackView addSubview:_label2]; //秒 _secondsLabel=[[UILabel alloc] initWithFrame:CGRectMake(_label2.frame.origin.x+_label2.frame.size.width, _hoursLabel.frame.origin.y, 20 , countdownBackView.frame.size.height)]; [countdownBackView addSubview:_secondsLabel]; _secondsLabel.font = [UIFont systemFontOfSize:11]; _label3=[[UILabel alloc] initWithFrame:CGRectMake(_secondsLabel.frame.origin.x+_secondsLabel.frame.size.width, _hoursLabel.frame.origin.y, 8 , countdownBackView.frame.size.height)]; [countdownBackView addSubview:_label3]; _millionSecondsLabel=[[UILabel alloc] initWithFrame:CGRectMake(_label3.frame.origin.x+_label3.frame.size.width, _hoursLabel.frame.origin.y, 20, countdownBackView.frame.size.height)]; [countdownBackView addSubview:_millionSecondsLabel]; //毫秒 _millionSecondsLabel.font = [UIFont systemFontOfSize: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; } return self; }
生成一个计时器
//得到未来某个日期的时间戳,与当前时间戳相比,得到两者的时间差,生成定时器 - (void)setTimeInterval:(double)timeInterval { _timeInterval = timeInterval ; NSDateFormatter *dataFormatter = [[NSDateFormatter alloc] init]; dataFormatter.dateFormat = @"MM/dd/yyyy HH:mm:ss.SSS"; //获取当前系统的时间,并用相应的格式转换 [dataFormatter stringFromDate:[NSDate date]]; NSString *currentDayStr = [dataFormatter stringFromDate:[NSDate date]]; NSDate *currentDate = [dataFormatter dateFromString:currentDayStr]; //优惠结束的时间,也用相同的格式去转换 NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeInterval/1000.0]; NSString *deadlineStr = [dataFormatter stringFromDate:date]; NSDate *deadlineDate = [dataFormatter dateFromString:deadlineStr]; _timeInterval=[deadlineDate timeIntervalSinceDate:currentDate]*1000; if (_timeInterval!=0) { //时间间隔是100毫秒,也就是0.1秒 _timer = [NSTimer scheduledTimerWithTimeInterval:0.1f target:self selector:@selector(timerAction) userInfo:nil repeats:YES]; [[NSRunLoop currentRunLoop] addTimer:_timer forMode:UITrackingRunLoopMode]; }else{ [countdownBackView removeFromSuperview]; } }
实现每隔100毫秒执行的方法,更新倒计时器上面相应的数值
// 每间隔100毫秒定时器触发执行该方法 - (void)timerAction { [self getTimeFromTimeInterval:_timeInterval] ; // 当时间间隔为0时干掉定时器 if (_timeInterval-_passTime == 0) { [_timer invalidate] ; _timer = nil ; } } // 通过时间间隔计算具体时间(小时,分,秒,毫秒) - (void)getTimeFromTimeInterval : (double)timeInterval { //1s=1000毫秒 _passTime += 100.f;//毫秒数从0-9,所以每次过去100毫秒 _tipLabel.text=@"还剩:"; _label3.text=@"."; _label2.text=@":"; _label1.text=@":"; //小时数 NSString *hours = [NSString stringWithFormat:@"%ld", (NSInteger)((timeInterval-_passTime)/1000/60/60)]; //分钟数 NSString *minute = [NSString stringWithFormat:@"%ld", (NSInteger)((timeInterval-_passTime)/1000/60)%60]; //秒数 NSString *second = [NSString stringWithFormat:@"%ld", ((NSInteger)(timeInterval-_passTime))/1000%60]; //毫秒数 CGFloat sss = ((NSInteger)((timeInterval - _passTime)))%1000/100; NSString *ss = [NSString stringWithFormat:@"%.lf", sss]; if (minute.integerValue < 10) { minute = [NSString stringWithFormat:@"0%@", minute]; } self.hoursLabel.text = [NSString stringWithFormat:@"%@",hours]; self.minutesLabel.text = [NSString stringWithFormat:@"%@",minute]; self.secondsLabel.text = [NSString stringWithFormat:@"%@",second]; self.millionSecondsLabel.text = [NSString stringWithFormat:@"%@",ss]; if (timeInterval - _passTime <= 0) { [countdownBackView removeFromSuperview]; [self removeFromSuperview]; } }
三、在ViewController.m给倒计时器赋值,实现自己想要的倒计时
- (void)viewDidLoad { [super viewDidLoad]; msecView=[[MsecCountDownView alloc] initWithFrame:CGRectMake(50, 100, self.view.frame.size.width-100, 16)]; [self.view addSubview:msecView]; NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateStyle:NSDateFormatterMediumStyle]; [formatter setTimeStyle:NSDateFormatterShortStyle]; [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSS"]; NSDate* date = [formatter dateFromString:@"2017-04-11 15:10:00.000"]; //将日期转换成时间戳 NSInteger timeSp = [[NSNumber numberWithDouble:[date timeIntervalSince1970]] integerValue]*1000; msecView.timeInterval=timeSp; }
这样就实现倒计时的功能了。但是使用倒计时还需要注意一点,当离开该页面的时候,记得把定时器暂停,等回到该页面的时候再启动倒计时。
这个可以通过以下两方法实现。
-(void)viewWillAppear:(BOOL)animated{ // 页面出现时,开启计时器 [msecView.timer setFireDate:[NSDate distantPast]]; } -(void)viewWillDisappear:(BOOL)animated{ // 页面消失时,暂停提示器 [msecView.timer setFireDate:[NSDate distantFuture]]; }
关于“iOS如何实现毫秒倒计时”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。
另外有需要云服务器可以了解下创新互联建站www.cdcxhl.com,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
文章标题:iOS如何实现毫秒倒计时-创新互联
文章路径:http://azwzsj.com/article/ppecj.html