溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

iOS如何實(shí)現(xiàn)毫秒倒計時

發(fā)布時間:2021-06-30 13:36:25 來源:億速云 閱讀:346 作者:小新 欄目:移動開發(fā)

這篇文章將為大家詳細(xì)講解有關(guān)iOS如何實(shí)現(xiàn)毫秒倒計時,小編覺得挺實(shí)用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

實(shí)現(xiàn)方法

自定義一個UIview,將倒計時封裝起來。

一、在MsecCountDownView.h中增加時間戳和計時器這兩屬性

@interface MsecCountDownView : UIView

@property(nonatomic, assign)double timeInterval;//未來某個日期的時間戳
@property(nonatomic, strong)NSTimer *timer ; //定時器

@end

二、在MsecCountDownView.m實(shí)現(xiàn)相關(guān)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

創(chuàng)建相關(guān)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;
}

生成一個計時器

//得到未來某個日期的時間戳,與當(dāng)前時間戳相比,得到兩者的時間差,生成定時器
- (void)setTimeInterval:(double)timeInterval
{

 _timeInterval = timeInterval ;

 NSDateFormatter *dataFormatter = [[NSDateFormatter alloc] init];
 dataFormatter.dateFormat = @"MM/dd/yyyy HH:mm:ss.SSS";

 //獲取當(dāng)前系統(tǒng)的時間,并用相應(yīng)的格式轉(zhuǎn)換
 [dataFormatter stringFromDate:[NSDate date]];
 NSString *currentDayStr = [dataFormatter stringFromDate:[NSDate date]];
 NSDate *currentDate = [dataFormatter dateFromString:currentDayStr];

 //優(yōu)惠結(jié)束的時間,也用相同的格式去轉(zhuǎn)換
 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];

 }

}

實(shí)現(xiàn)每隔100毫秒執(zhí)行的方法,更新倒計時器上面相應(yīng)的數(shù)值

// 每間隔100毫秒定時器觸發(fā)執(zhí)行該方法
- (void)timerAction
{

 [self getTimeFromTimeInterval:_timeInterval] ;


 // 當(dāng)時間間隔為0時干掉定時器
 if (_timeInterval-_passTime == 0)
 {
  [_timer invalidate] ;
  _timer = nil ;
 }
}

// 通過時間間隔計算具體時間(小時,分,秒,毫秒)
- (void)getTimeFromTimeInterval : (double)timeInterval
{

 //1s=1000毫秒
 _passTime += 100.f;//毫秒數(shù)從0-9,所以每次過去100毫秒
 _tipLabel.text=@"還剩:";

 _label3.text=@".";
 _label2.text=@":";
 _label1.text=@":";

 //小時數(shù)
 NSString *hours = [NSString stringWithFormat:@"%ld", (NSInteger)((timeInterval-_passTime)/1000/60/60)];
 //分鐘數(shù)
 NSString *minute = [NSString stringWithFormat:@"%ld", (NSInteger)((timeInterval-_passTime)/1000/60)%60];
 //秒數(shù)
 NSString *second = [NSString stringWithFormat:@"%ld", ((NSInteger)(timeInterval-_passTime))/1000%60];
 //毫秒數(shù)
 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給倒計時器賦值,實(shí)現(xiàn)自己想要的倒計時

- (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"];
 //將日期轉(zhuǎn)換成時間戳
 NSInteger timeSp = [[NSNumber numberWithDouble:[date timeIntervalSince1970]] integerValue]*1000;

 msecView.timeInterval=timeSp;

}

這樣就實(shí)現(xiàn)倒計時的功能了。但是使用倒計時還需要注意一點(diǎn),當(dāng)離開該頁面的時候,記得把定時器暫停,等回到該頁面的時候再啟動倒計時。

這個可以通過以下兩方法實(shí)現(xiàn)。

-(void)viewWillAppear:(BOOL)animated{

// 頁面出現(xiàn)時,開啟計時器 
 [msecView.timer setFireDate:[NSDate distantPast]];

}
-(void)viewWillDisappear:(BOOL)animated{
// 頁面消失時,暫停提示器
 [msecView.timer setFireDate:[NSDate distantFuture]];
}

關(guān)于“iOS如何實(shí)現(xiàn)毫秒倒計時”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

ios
AI