我想显示倒计时器。我有开始日期和结束日期。我需要显示剩余的时间,如
天:小时:分钟:秒
我该怎么做?
答案 0 :(得分:18)
你可以像我的下面的代码一样设置倒计时: -
-(void) viewWillAppear:(BOOL)animated
{
timer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target:self selector:@selector(updateCountdown) userInfo:nil repeats: YES];
}
和强>
-(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)];
}
现在只需设置你的逻辑
其代码输出为我的项目如下:: -
答案 1 :(得分:5)
这是.h文件的代码:
@interface UIMyContoller : UIViewController {
NSTimer *timer;
IBOutlet UILabel *myCounterLabel;
}
@property (nonatomic, retain) UILabel *myCounterLabel;
-(void)updateCounter:(NSTimer *)theTimer;
-(void)countdownTimer;
@end
这是.m文件的代码:
@implementation UIMyController
@synthesize myCounterLabel;
int hours, minutes, seconds;
int secondsLeft;
- (void)viewDidLoad {
[super viewDidLoad];
secondsLeft = 16925;
[self countdownTimer];
}
- (void)updateCounter:(NSTimer *)theTimer {
if(secondsLeft > 0 ){
secondsLeft -- ;
hours = secondsLeft / 3600;
minutes = (secondsLeft % 3600) / 60;
seconds = (secondsLeft %3600) % 60;
myCounterLabel.text = [NSString stringWithFormat:@"%02d:%02d:%02d", hours, minutes, seconds];
}
else{
secondsLeft = 16925;
}
}
-(void)countdownTimer{
secondsLeft = hours = minutes = seconds = 0;
if([timer isValid])
{
[timer release];
}
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(updateCounter:) userInfo:nil repeats:YES];
[pool release];
}
希望这会有所帮助。快乐的编码
阿德里安
答案 2 :(得分:2)
我建议使用以下方法获取From Date到To Date的时间组件。
这种方法比手动递减3小时,分钟和秒的3个整数变量更优雅,因为这样做意味着你必须手动检查第二次命中0,你需要手动将分钟重置回59,依此类推。我曾沿着这条路走过一次,这不太好。
此外,当您最小化您的应用时,倒计时时钟将停止。如果您使用3个整数(小时,分钟和秒)手动递减倒计时器,那么最小化您的应用将导致倒计时搞砸。
由于此方法会自动计算两个日期之间的差异,即使应用程序从后台最小化状态返回,它也会自动重新计算剩余时间,而无需任何额外代码。
-(void)viewDidLoad
{
// instantiate a calendar object.
gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
countDownTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateClock:) userInfo:nil repeats:YES];
[countDownTimer fire];
}
-(void)updateClock:(NSTimer *)timer
{
countDownDateFormatter = [[NSDateFormatter alloc] init];
[countDownDateFormatter setDateFormat:@"hh:mm:ss"];
NSDate *now = [NSDate date];
NSDateComponents *comp = [gregorianCalendar components:NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit
fromDate:now
toDate:countDownEndDate
options:0];
NSString *strTimeRemaining = nil;
// if date have not expired
if([now compare:countDownEndDate] == NSOrderedAscending)
{
strTimeRemaining = [[NSString alloc] initWithFormat:@"%02d:%02d:%02d", [comp hour], [comp minute], [comp second]];
}
else
{
// time has expired, set time to 00:00 and set boolean flag to no
strTimeRemaining = [[NSString alloc] initWithString:@"00:00:00"];
[countDownTimer invalidate];
countDownTimer = nil;
}
lblCountDown.text = strTimeRemaining;
[countDownDateFormatter release];
[strTimeRemaining release];
}