介绍
小编给大家分享一下iOS如何实现秒杀活动倒计时,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获、下面让我们一起去了解一下吧!
iOS关于大型网站抢购,距活动结束,剩余时间倒计时的实现代码,代码比较简单,大家根据需求适当的添加修改删除代码
1。定义4个标签来接收倒计时:
@property (弱,,原子),IBOutlet UILabel * dayLabel; @property (弱,,原子),IBOutlet UILabel * hourLabel; @property (弱,,原子),IBOutlet UILabel * minuteLabel; @property (弱,,原子),IBOutlet UILabel * secondLabel;
2。在实现文件中实现方法:
//时间戳转换为日期格式(毫秒的时间戳) 安康;(NSString *) timeWithTimeIntervalString:(NSString *) timeString {//才能,格式化时间 NSDateFormatter *,才能formatter =, [[NSDateFormatter alloc], init); formatter.timeZone 才能=,(NSTimeZone timeZoneWithName: @" shanghai"); (才能formatter setDateStyle NSDateFormatterMediumStyle):; (才能formatter setTimeStyle NSDateFormatterShortStyle):; (才能formatter setDateFormat: @" yyyy-MM-dd HH: mm: ss"];//才能,毫秒值转化为秒 NSDate *,才能date =, (NSDate dateWithTimeIntervalSince1970: [timeString doubleValue]/, 1000.0); NSString *,才能dateString =, (formatter stringFromDate:日期); NSLog才能(@"时间,===,% @", dateString); return 才能;dateString; } - (void) downSecondHandle:(NSString *) aTimeString { NSDateFormatter 才能;* dateformat=[[NSDateFormatter alloc], init); (才能dateFormatter setDateFormat: @" yyyy-MM-dd HH: mm: ss"]; NSDate 才能;* endDate =, (dateFormatter dateFromString: [self timeWithTimeIntervalString: aTimeString]];,//结束时间 NSDate 才能;* endDate_tomorrow =, [[NSDate alloc], initWithTimeIntervalSinceReferenceDate: ([endDate timeIntervalSinceReferenceDate])]; NSDate 才能;* startDate =, (NSDate 日期); ,,,NSString *, dateString =, (dateFormatter stringFromDate: startDate可以); NSLog才能(@"现在的时间,===,% @", dateString); NSTimeInterval 才能;timeInterval =(endDate_tomorrow timeIntervalSinceDate: startDate可以); if 才能;(_timer==nil), { ,,,__block int timeout =, timeInterval;,//倒计时时间 ,,,if (超时!=0),{ ,,,,,dispatch_queue_t queue =, dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); ,,,,,_timer =, dispatch_source_create (DISPATCH_SOURCE_TYPE_TIMER, 0, 0,队列); ,,,,,dispatch_source_set_timer (_timer, dispatch_walltime (NULL,, 0), 1.0 * NSEC_PER_SEC, 0),,//每秒执行 ,,,,,dispatch_source_set_event_handler (_timer,, ^ { ,,,,,,,如果(timeout<=0){,//倒计时结束,关闭 ,,,,,,,,,dispatch_source_cancel (_timer); ,,,,,,,,,_timer =,零; ,,,,,,,,,设置(dispatch_get_main_queue (),, ^ { ,,,,,,,,,,,self.dayLabel.text =, @"“; ,,,,,,,,,,,self.hourLabel.text =, @" 00“;; ,,,,,,,,,,,self.minuteLabel.text =, @" 00“;; ,,,,,,,,,,,self.secondLabel.text =, @" 00“;; ,,,,,,,,,}); ,,,,,,,其他}{ ,,,,,,,,,int days =, (int)(超时/(3600 * 24)); ,,,,,,,,,if (天==0),{ ,,,,,,,,,,,self.dayLabel.text =, @"“; ,,,,,,,,,} ,,,,,,,,,int hours =, (int) (timeout-days * 24 * 3600)/(3600); ,,,,,,,,,int minute =, (int) (timeout-days * 24 * 3600 -小时* 3600)/60; ,,,,,,,,,int second =, timeout-days * 24 * 3600 -小时* 3600 * 60分钟; ,,,,,,,,,设置(dispatch_get_main_queue (),, ^ { null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null nulliOS如何实现秒杀活动倒计时