欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

iOS實現(xiàn)毫秒倒計時的方法詳解

 更新時間:2017年04月23日 16:34:44   作者:若錦  
倒計時在我們?nèi)粘i_發(fā)中必不可少,最近在公司的一個項目中就遇到了這個需求,本文著重介紹的是利用iOS實現(xiàn)毫秒倒計時的方法,文中給出了詳細的示例代碼,需要的朋友可以參考借鑒,下面來一起學習學習吧。

前言

大家應該都知道在app開發(fā)中,當展示限時優(yōu)惠的某些商品時,往往會加一個倒計時,提示用戶該商品限時優(yōu)惠所剩的時間,。那對于開發(fā)者來說,這就需要我們?nèi)崿F(xiàn)的是一個倒計時的功能,這個倒計時根據(jù)具體需求,可以以天、小時、分、秒、毫秒作單位。

今天呢,主要說說毫秒計時器。我們知道秒和毫秒之間的進制是1000,也就是說1秒=1000毫秒,那我們做毫秒倒計時器的時候是設置一個時間間隔為1毫秒的計時器,逐一減少毫秒數(shù)。但是這樣的話太耗時了,所以很多的毫秒計時器中的毫秒數(shù)只是0-9之間的數(shù)字,這就意味著,這個毫秒計時器的時間間隔是100毫秒,這樣相比起1毫秒為間隔的計時器,其消耗就少了很多,同時也達到毫秒計時的效果。

那對于整個毫秒倒計時的實現(xiàn)思路就是:得到未來某個日期的時間戳和當前日期的時間戳,計算這兩者之間的時間差,然后設置一個時間間隔為100毫秒的計時器,每隔100毫秒,更新一下倒計時器上相應的數(shù)值。

實現(xiàn)方法

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

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

@interface MsecCountDownView : UIView

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

@end

二、在MsecCountDownView.m實現(xià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)建相關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";

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

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

 }

}

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

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

 [self getTimeFromTimeInterval:_timeInterval] ;


 // 當時間間隔為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給倒計時器賦值,實現(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"];
 //將日期轉換成時間戳
 NSInteger timeSp = [[NSNumber numberWithDouble:[date timeIntervalSince1970]] integerValue]*1000;

 msecView.timeInterval=timeSp;

}

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

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

-(void)viewWillAppear:(BOOL)animated{

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

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

如有需要,可通過下面兩種方法下載demo

一:GitHub上下載

二:本地下載

總結

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。

相關文章

  • iOS中設置父視圖透明但內(nèi)容不透明的方法

    iOS中設置父視圖透明但內(nèi)容不透明的方法

    設置一定的背景透明會讓用戶的體驗非常不錯,下面這篇文章就主要跟大家分享了iOS中設置父視圖透明但內(nèi)容不透明的方法,文中給出了詳細的示例代碼,需要的朋友們下面來一起看看吧。
    2017-05-05
  • iOS 下的圖片處理與性能優(yōu)化詳解

    iOS 下的圖片處理與性能優(yōu)化詳解

    這篇文章主要介紹了iOS 下的圖片處理與性能優(yōu)化詳解,幫助大家更好的理解和學習使用ios開發(fā),感興趣的朋友可以了解下
    2021-04-04
  • iOS組件封裝與自動布局自定義表情鍵盤

    iOS組件封裝與自動布局自定義表情鍵盤

    這篇文章主要介紹了iOS組件封裝與自動布局自定義表情鍵盤的相關資料,需要的朋友可以參考下
    2016-04-04
  • iOS12新特性之推送通知詳解

    iOS12新特性之推送通知詳解

    這篇文章主要給大家介紹了關于iOS12新特性之推送通知的相關資料文中通過圖文以及示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2018-06-06
  • iOS開發(fā)————詳解適配iOS10問題

    iOS開發(fā)————詳解適配iOS10問題

    ios10已經(jīng)推出一段時間了,這篇文章主要介紹了iOS開發(fā)————詳解適配iOS10,有興趣的可以了解一下。
    2016-12-12
  • Objective-C 代碼與Javascript 代碼相互調(diào)用實例

    Objective-C 代碼與Javascript 代碼相互調(diào)用實例

    這篇文章主要介紹了Objective-C 代碼與Javascript 代碼相互調(diào)用實例的相關資料,現(xiàn)在的APP 應用有時候會調(diào)用網(wǎng)頁上的內(nèi)容,為了增加用戶體驗,這里寫下個實例,需要的朋友可以參考下
    2016-10-10
  • iOS客戶端本地推送實現(xiàn)代碼

    iOS客戶端本地推送實現(xiàn)代碼

    這篇文章主要介紹了iOS客戶端本地推送實現(xiàn)代碼,并確定程序中只有一個彈出框,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-11-11
  • IOS開發(fā)自定義view方法規(guī)范示例

    IOS開發(fā)自定義view方法規(guī)范示例

    這篇文章主要為大家介紹了IOS開發(fā)自定義view方法規(guī)范示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-07-07
  • 淺談iphone X的簡單適配問題(推薦)

    淺談iphone X的簡單適配問題(推薦)

    這篇文章主要介紹了淺談iphone X的簡單適配(推薦),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-11-11
  • 基于ios逆向過程中l(wèi)ldb調(diào)試技巧(推薦)

    基于ios逆向過程中l(wèi)ldb調(diào)試技巧(推薦)

    下面小編就為大家?guī)硪黄趇os逆向過程中l(wèi)ldb調(diào)試技巧(推薦)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-07-07

最新評論