倒数计时器 – iPhone

我想显示倒数计时器。 我有开始date和结束date。 我需要显示剩余的时间

天:小时:分钟:秒

我怎样才能做到这一点?

你可以像我的下面的代码设置coundown: –

-(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)]; } 

现在只需设置你的逻辑

它的代码输出为我的项目如下:: –

在这里输入图像说明

这是.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]; } 

希望这可以帮助。 快乐的编码

阿德里安

我会推荐下面的方法获取时间组件从开始date到date。

此方法比手动递减3个整数variables(小时,分钟和秒)更为优雅,因为这样做意味着您必须手动检查第二次点击0的时间,您需要手动将分钟重置为59等等。 我一次下了这条路,不是很好。

另外,当你最小化你的应用程序,倒数时钟将停止。 如果您使用3个整数(小时,分钟和秒)手动递减计时器,最小化您的应用程序将导致计数下降。

由于此方法会自动计算两个date之间的差异,即使应用程序从背景最小化状态返回时,也会自动重新计算剩余时间,无需任何额外的代码。

 -(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]; }