5

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

天:时:分:秒

我怎样才能做到这一点?

4

3 回答 3

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



 }

现在只需设置您的逻辑

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

在此处输入图像描述

于 2012-11-22T05:05:48.120 回答
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];
}

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

阿德里安

于 2012-11-22T04:41:40.643 回答
2

我会推荐以下方法来获取从日期到日期的时间分量。

这种方法比手动减少小时、分钟和秒的 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];
}
于 2012-11-22T05:33:56.637 回答