美文网首页
iOS 两种简单的倒计时

iOS 两种简单的倒计时

作者: 叶飞楼 | 来源:发表于2016-10-24 14:28 被阅读0次

1.
{

NSTimer *timer;

}

- (void)viewDidLoad {

timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timeMethodGo:) userInfo:nil repeats:YES];

}


- (void)timeMethodGo:(NSTimer *)theTimer {

NSCalendar *calender = [NSCalendar currentCalendar];

NSDate *startDate = [NSDate date];

NSDateFormatter *foamatter = [[NSDateFormatter alloc] init];

[foamatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

NSDate *endDate = [foamatter dateFromString:@"2016-10-25 00:00:00"];

unsigned int unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth |NSCalendarUnitDay |NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond ;

NSDateComponents *day = [calender components:unitFlags fromDate:startDate toDate:endDate options:0];

_dayLabel.text = [NSString stringWithFormat:@"%ld", (long)[day day]];

_hourLabel.text = [NSString stringWithFormat:@"%ld", (long)[day hour]];

_minuteLabel.text = [NSString stringWithFormat:@"%ld", (long)[day minute]];

_secondLabel.text = [NSString stringWithFormat:@"%ld", (long)[day second]];

if ([day day] == 0 && [day hour] == 0 && [day minute] == 0 && [day second] == 0) {

[theTimer setFireDate:[NSDate distantFuture]];

}

}

2.

{

dispatch_source_t _timer;

}

- (void)viewDidLoad {

NSDate *startDate = [NSDate date];

NSDateFormatter *foamatter = [[NSDateFormatter alloc] init];

[foamatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

NSDate *endDate = [foamatter dateFromString:@"2017-10-24 00:00:00"];

NSTimeInterval timeInterval =[endDate timeIntervalSinceDate:startDate];

if (_timer==nil) {

__block int timeout = timeInterval; //倒计时时间

if (timeout!=0) {

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

_timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);

dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行

dispatch_source_set_event_handler(_timer, ^{

if(timeout<=0){ //倒计时结束,关闭

dispatch_source_cancel(_timer);

_timer = nil;

dispatch_async(dispatch_get_main_queue(), ^{

_dayLabel.text = @"";

_hourLabel.text = @"00";

_minuteLabel.text = @"00";

_secondLabel.text = @"00";

});

} else {

int days = (int)(timeout/(3600*24));

if (days==0) {

_dayLabel.text = @"";

}

int hours = (int)((timeout-days*24*3600)/3600);

int minute = (int)(timeout-days*24*3600-hours*3600)/60;

int second = timeout-days*24*3600-hours*3600-minute*60;

dispatch_async(dispatch_get_main_queue(), ^{

if (days==0) {

_dayLabel.text = @"0天";

} else {

_dayLabel.text = [NSString stringWithFormat:@"%d天",days];

}

if (hours<10) {

_hourLabel.text = [NSString stringWithFormat:@"0%d",hours];

} else {

_hourLabel.text = [NSString stringWithFormat:@"%d",hours];

}

if (minute<10) {

_minuteLabel.text = [NSString stringWithFormat:@"0%d",minute];

} else {

_minuteLabel.text = [NSString stringWithFormat:@"%d",minute];

}

if (second<10) {

_secondLabel.text = [NSString stringWithFormat:@"0%d",second];

} else {

_secondLabel.text = [NSString stringWithFormat:@"%d",second];

}

});

timeout--;

}

});

dispatch_resume(_timer);

}

}

}

}

相关文章

  • 倒计时

    ios怎么在cell上添加倒计时 iOS中 简单易懂的秒杀倒计时/倒计时 iOS开发-三种倒计时的写法 iOS实现...

  • iOS 两种简单的倒计时

    1.{ NSTimer *timer; }- (void)viewDidLoad { timer = [NSTim...

  • IOS表格的移动和删除

    简介ios表格有UICollectionView、UITableView两种格式,下面简单的整理了一下这两种表格的...

  • iOS 方便的AttributedString生成

    本文提供两种简单的AttributedString的生成方式   iOS中的NSAttributedString一...

  • ios简单的倒计时实现

    /** * 获取当天的年月日的字符串 * 这里测试用 * @return 格式为年-月-日 时分秒 */ -(NS...

  • iOS 简单的环形倒计时

    提到倒计时或者时进度条 现在总有很多种样式 上一次我们搞了双曲线波浪 进度http://www.jianshu.c...

  • iOS 动画

    一、动画基本知识 1. iOS动画(或者所有动画)的原理简单来讲有两种,是哪两种? ① 告诉系统动画对象在某几个时...

  • iOS实现倒计时的两种方式

    `新手一枚,此文章只为记录程序人生的点点滴滴,希望大家能够提点一下,谢谢!!谢谢!!!谢谢!!! 1.使用NSTi...

  • iOS倒计时的两种实现方式

    今天项目中遇到获取验证码之后有一个倒计时功能的实现,百度了一下,有两种实现方式的,再次总结一下,以便日后需要查看,...

  • iOS 开发 简单的倒计时实现

    项目中有一个支付时间倒计时的需求,类似于美团外卖的支付倒计时。我也从网上搜到一些实现的方法,以下是我总结的一些。 ...

网友评论

      本文标题:iOS 两种简单的倒计时

      本文链接:https://www.haomeiwen.com/subject/xjdluttx.html