老熟女激烈的高潮_日韩一级黄色录像_亚洲1区2区3区视频_精品少妇一区二区三区在线播放_国产欧美日产久久_午夜福利精品导航凹凸

重慶分公司,新征程啟航

為企業(yè)提供網(wǎng)站建設(shè)、域名注冊(cè)、服務(wù)器等服務(wù)

iOS如何實(shí)現(xiàn)毫秒倒計(jì)時(shí)-創(chuàng)新互聯(lián)

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

為連云港等地區(qū)用戶提供了全套網(wǎng)頁(yè)設(shè)計(jì)制作服務(wù),及連云港網(wǎng)站建設(shè)行業(yè)解決方案。主營(yíng)業(yè)務(wù)為成都網(wǎng)站建設(shè)、做網(wǎng)站、連云港網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠(chéng)的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會(huì)得到認(rèn)可,從而選擇與我們長(zhǎng)期合作。這樣,我們也可以走得更遠(yuǎn)!

實(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ān)于“iOS如何實(shí)現(xiàn)毫秒倒計(jì)時(shí)”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)建站www.cdcxhl.com,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。


文章標(biāo)題:iOS如何實(shí)現(xiàn)毫秒倒計(jì)時(shí)-創(chuàng)新互聯(lián)
URL分享:http://www.xueling.net.cn/article/ppecj.html

其他資訊

在線咨詢
服務(wù)熱線
服務(wù)熱線:028-86922220
TOP
主站蜘蛛池模板: 日本一区二区久久久 | 忘忧草在线影院www日本 | 欧美牲交videossexeso | 青青草视频在线免费播放 | 永夜星河在线免费观看 | 人人妻人人妻人人人人妻人人 | 免费一级性片 | 精品国产乱码久久久久久108 | 在线观看国产一区 | 日本激情视频网站 | 亚洲精品国产成人片 | 日本一道本线一区免费 | 国产成人精品日本亚洲18 | 国产精品宾馆在线精品酒店 | 国产网站视频 | 国产成人精品无码一区二区蜜柚 | 成人羞羞国产 | 中文字幕精品无码一区二区三区 | 日韩亚洲国产精品 | 亚洲人成无码网站18禁 | 欧美成人综合视频 | 一本AV高清一区二区三区 | 亚洲AV无码片区一区二区三区 | 天堂久久久久 | 国产亲子乱弄免费视频 | 看片网站在线观看 | 一区二区美女视频 | 亚洲国产精品福利 | 亚洲色偷偷综合亚洲AV伊人蜜桃 | 国产乱XXXXX97国语对白 | 亚洲精品无码AV中文字幕 | 全黄H全肉边做边吃奶视频 9999在线视频 | 国产精品久久久久久亚洲美女高潮 | 午夜寂寞视频无码专区 | 亚洲6080yy久久无码国产 | 九九热视频免费 | 免费观看欧美日韩亚洲 | 人与人一级毛片 | 日本黄色毛片 | 亚洲一中文字幕 | 麻豆网站 |