倒数计时器 - iPhone [英] Count down timer - iPhone
本文介绍了倒数计时器 - iPhone的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想显示倒计时器。我有开始日期和结束日期。我需要显示剩余时间,如
I want to display count down timer. I have the start date and end date. I need to display the remaining time like
天:小时:分钟:秒
我该怎么做?
推荐答案
你可以像下面的代码一样设置coundown: -
you can set coundown like my below code :-
-(void) viewWillAppear:(BOOL)animated
{
timer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target:self selector:@selector(updateCountdown) userInfo:nil repeats: YES];
}
AND
-(void) updateCountdown
{
NSString *dateString = @"14-12-2012";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd-MM-yyyy"];
NSDate *dateFromString = [[NSDate alloc] init];
// voila!
dateFromString = [dateFormatter dateFromString:dateString];
NSDate *now = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *componentsHours = [calendar components:NSHourCalendarUnit fromDate:now];
NSDateComponents *componentMint = [calendar components:NSMinuteCalendarUnit fromDate:now];
NSDateComponents *componentSec = [calendar components:NSSecondCalendarUnit fromDate:now];
NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *componentsDaysDiff = [gregorianCalendar components:NSDayCalendarUnit
fromDate:now
toDate:dateFromString
options:0];
lblDaysSetting.text=[NSString stringWithFormat:@"%02d",componentsDaysDiff.day];
lblHouresSetting.text=[NSString stringWithFormat:@"%02d",(24-componentsHours.hour)];
lblMinitSetting.text=[NSString stringWithFormat:@"%02d",(60-componentMint.minute)];
lblSecSetting.text=[NSString stringWithFormat:@"%02d",(60-componentSec.second)];
}
现在只需设置你的逻辑
其代码输出为我的项目如下: -
its code output as my project as bellow::-
这篇关于倒数计时器 - iPhone的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文