iOS實(shí)現(xiàn)毫秒倒計(jì)時(shí)的方法詳解
前言
大家應(yīng)該都知道在app開(kāi)發(fā)中,當(dāng)展示限時(shí)優(yōu)惠的某些商品時(shí),往往會(huì)加一個(gè)倒計(jì)時(shí),提示用戶(hù)該商品限時(shí)優(yōu)惠所剩的時(shí)間,。那對(duì)于開(kāi)發(fā)者來(lái)說(shuō),這就需要我們?nèi)?shí)現(xiàn)的是一個(gè)倒計(jì)時(shí)的功能,這個(gè)倒計(jì)時(shí)根據(jù)具體需求,可以以天、小時(shí)、分、秒、毫秒作單位。
今天呢,主要說(shuō)說(shuō)毫秒計(jì)時(shí)器。我們知道秒和毫秒之間的進(jìn)制是1000,也就是說(shuō)1秒=1000毫秒,那我們做毫秒倒計(jì)時(shí)器的時(shí)候是設(shè)置一個(gè)時(shí)間間隔為1毫秒的計(jì)時(shí)器,逐一減少毫秒數(shù)。但是這樣的話(huà)太耗時(shí)了,所以很多的毫秒計(jì)時(shí)器中的毫秒數(shù)只是0-9之間的數(shù)字,這就意味著,這個(gè)毫秒計(jì)時(shí)器的時(shí)間間隔是100毫秒,這樣相比起1毫秒為間隔的計(jì)時(shí)器,其消耗就少了很多,同時(shí)也達(dá)到毫秒計(jì)時(shí)的效果。
那對(duì)于整個(gè)毫秒倒計(jì)時(shí)的實(shí)現(xiàn)思路就是:得到未來(lái)某個(gè)日期的時(shí)間戳和當(dāng)前日期的時(shí)間戳,計(jì)算這兩者之間的時(shí)間差,然后設(shè)置一個(gè)時(shí)間間隔為100毫秒的計(jì)時(shí)器,每隔100毫秒,更新一下倒計(jì)時(shí)器上相應(yīng)的數(shù)值。
實(shí)現(xiàn)方法
自定義一個(gè)UIview,將倒計(jì)時(shí)封裝起來(lái)。
一、在MsecCountDownView.h中增加時(shí)間戳和計(jì)時(shí)器這兩屬性
@interface MsecCountDownView : UIView @property(nonatomic, assign)double timeInterval;//未來(lái)某個(gè)日期的時(shí)間戳 @property(nonatomic, strong)NSTimer *timer ; //定時(shí)器 @end
二、在MsecCountDownView.m實(shí)現(xiàn)相關(guān)UI及倒計(jì)時(shí)方法
@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];
//小時(shí)
_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;
}
生成一個(gè)計(jì)時(shí)器
//得到未來(lái)某個(gè)日期的時(shí)間戳,與當(dāng)前時(shí)間戳相比,得到兩者的時(shí)間差,生成定時(shí)器
- (void)setTimeInterval:(double)timeInterval
{
_timeInterval = timeInterval ;
NSDateFormatter *dataFormatter = [[NSDateFormatter alloc] init];
dataFormatter.dateFormat = @"MM/dd/yyyy HH:mm:ss.SSS";
//獲取當(dāng)前系統(tǒng)的時(shí)間,并用相應(yīng)的格式轉(zhuǎn)換
[dataFormatter stringFromDate:[NSDate date]];
NSString *currentDayStr = [dataFormatter stringFromDate:[NSDate date]];
NSDate *currentDate = [dataFormatter dateFromString:currentDayStr];
//優(yōu)惠結(jié)束的時(shí)間,也用相同的格式去轉(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)
{
//時(shí)間間隔是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í)行的方法,更新倒計(jì)時(shí)器上面相應(yīng)的數(shù)值
// 每間隔100毫秒定時(shí)器觸發(fā)執(zhí)行該方法
- (void)timerAction
{
[self getTimeFromTimeInterval:_timeInterval] ;
// 當(dāng)時(shí)間間隔為0時(shí)干掉定時(shí)器
if (_timeInterval-_passTime == 0)
{
[_timer invalidate] ;
_timer = nil ;
}
}
// 通過(guò)時(shí)間間隔計(jì)算具體時(shí)間(小時(shí),分,秒,毫秒)
- (void)getTimeFromTimeInterval : (double)timeInterval
{
//1s=1000毫秒
_passTime += 100.f;//毫秒數(shù)從0-9,所以每次過(guò)去100毫秒
_tipLabel.text=@"還剩:";
_label3.text=@".";
_label2.text=@":";
_label1.text=@":";
//小時(shí)數(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給倒計(jì)時(shí)器賦值,實(shí)現(xiàn)自己想要的倒計(jì)時(shí)
- (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)換成時(shí)間戳
NSInteger timeSp = [[NSNumber numberWithDouble:[date timeIntervalSince1970]] integerValue]*1000;
msecView.timeInterval=timeSp;
}
這樣就實(shí)現(xiàn)倒計(jì)時(shí)的功能了。但是使用倒計(jì)時(shí)還需要注意一點(diǎn),當(dāng)離開(kāi)該頁(yè)面的時(shí)候,記得把定時(shí)器暫停,等回到該頁(yè)面的時(shí)候再啟動(dòng)倒計(jì)時(shí)。
這個(gè)可以通過(guò)以下兩方法實(shí)現(xiàn)。
-(void)viewWillAppear:(BOOL)animated{
// 頁(yè)面出現(xiàn)時(shí),開(kāi)啟計(jì)時(shí)器
[msecView.timer setFireDate:[NSDate distantPast]];
}
-(void)viewWillDisappear:(BOOL)animated{
// 頁(yè)面消失時(shí),暫停提示器
[msecView.timer setFireDate:[NSDate distantFuture]];
}
如有需要,可通過(guò)下面兩種方法下載demo
二:本地下載
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
- IOS開(kāi)發(fā)代碼分享之用nstimer實(shí)現(xiàn)倒計(jì)時(shí)功能
- IOS實(shí)現(xiàn)驗(yàn)證碼倒計(jì)時(shí)功能(一)
- IOS關(guān)于大型網(wǎng)站搶購(gòu)、距活動(dòng)結(jié)束,剩余時(shí)間倒計(jì)時(shí)的實(shí)現(xiàn)代碼
- ios 實(shí)現(xiàn)倒計(jì)時(shí)的兩種方式
- iOS中實(shí)現(xiàn)簡(jiǎn)單易懂秒殺倒計(jì)時(shí)/倒計(jì)時(shí)代碼
- iOS中讓多個(gè)cell上都出現(xiàn)倒計(jì)時(shí)的分析與實(shí)現(xiàn)
- iOS獲取短信驗(yàn)證碼倒計(jì)時(shí)的兩種實(shí)現(xiàn)方法
- iOS啟動(dòng)頁(yè)倒計(jì)時(shí)跳過(guò)按鈕功能
- Swift實(shí)現(xiàn)iOS應(yīng)用中短信驗(yàn)證碼倒計(jì)時(shí)功能的實(shí)例分享
- iOS實(shí)現(xiàn)秒殺活動(dòng)倒計(jì)時(shí)
相關(guān)文章
iOS應(yīng)用開(kāi)發(fā)中導(dǎo)航欄按鈕UIBarButtonItem的添加教程
這篇文章主要介紹了iOS應(yīng)用開(kāi)發(fā)中導(dǎo)航欄按鈕UIBarButtonItem的添加教程,文中詳細(xì)介紹了使用UINavigationController導(dǎo)航控制器添加的過(guò)程,需要的朋友可以參考下2016-02-02
IOS開(kāi)發(fā)相冊(cè)圖片多選和刪除的功能
之前小編有和大家分享過(guò)一篇關(guān)于從相冊(cè)選取單張照片的文章,那么下面這篇文章跟大家分享下如何相冊(cè)多圖選擇和刪除,以及包括拍照功能,有需要的可以參考學(xué)習(xí),下面來(lái)一起看看吧。2016-09-09
iOS Remote Notification遠(yuǎn)程消息推送處理
這篇文章主要為大家詳細(xì)介紹了iOS Remote Notification遠(yuǎn)程消息推送處理,感興趣的小伙伴們可以參考一下2016-09-09
解決iOS UITextField 編輯時(shí)文本偏移問(wèn)題
這篇文章主要介紹了解決iOS UITextField 編輯時(shí)文本偏移問(wèn)題,需要的朋友可以參考下2017-05-05
Flutter Widgets粘合劑CustomScrollView NestedScrollVie
這篇文章主要為大家介紹了Flutter Widgets粘合劑CustomScrollView NestedScrollView滾動(dòng)控件示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
IOS 通訊錄的訪(fǎng)問(wèn)和修改的實(shí)現(xiàn)
這篇文章主要介紹了IOS 通訊錄的訪(fǎng)問(wèn)和修改的實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下2017-06-06

