iOS倒计时器到特定日期 [英] iOS Countdown Timer To Specific Date

查看:89
本文介绍了iOS倒计时器到特定日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将倒计时器定时到特定日期。我使用:

I am trying to get a countdown timer to a specific date. I use:

-(void) viewWillAppear:(BOOL)animated {

    NSString *str =@"12/27/2013";
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"MM/dd/yyyy"];
    NSDate *date = [formatter dateFromString:str];

    NSTimeInterval numberOfSecondsUntilSelectedDate = [date timeIntervalSinceNow];
    NSInteger numberOfDays = numberOfSecondsUntilSelectedDate / 86400;


    startTime = [[NSDate date] retain];

    secondsLeft = numberOfDays;
    NSLog(@"number%f", numberOfSecondsUntilSelectedDate);
    [self countdownTimer];

}
- (void)updateCounter:(NSTimer *)theTimer {
    if(secondsLeft > 0 ){
        secondsLeft -- ;
        days = secondsLeft / 86400;
        NSLog(@"%d", days);
        hours = secondsLeft / 3600;
        minutes = (secondsLeft % 3600) / 60;
        seconds = (secondsLeft %3600) % 60;
        myCounterLabel.text = [NSString stringWithFormat:@"%02d Days %02d Hours %02d Minutes %02d Seconds", days, 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];
}

然而,这一天一直是0,我做错了什么?

However, the day keeps coming out as 0. What am I doing wrong?

推荐答案

计算到特定日期的方法是启动一个计时器并询问每次触发该日期是否有应验。第二个问题是如何计算UI之前的小时分钟秒。答案是NSDateFormatter - 绝对不是* 60或* 24数学。

The way to count down to a particular date is to just start a timer and ask each time it fires whether that date has come to pass. The second problem is how to calculate the hours minutes seconds until that time for the UI. The answer there is NSDateFormatter -- definitely not *60 or *24 math.

@property (strong, nonatomic) NSDate *targetDate;

// compute targetDate as you have it

// ...
self.targetDate = [formatter dateFromString:str]

// start a timer
[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(updateCounter:) userInfo:nil repeats:YES]; 

- (void)updateCounter:(NSTimer *)theTimer {

    NSDate *now = [NSDate date]; 
    // has the target time passed?
    if ([self.targetDate earlierDate:now] == self.targetDate) {
        [theTimer invalidate];
    } else {
        NSUInteger flags = NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
        NSDateComponents *components = [[NSCalendar currentCalendar] components:flags fromDate:now toDate:self.targetDate options:0];

        NSLog(@"there are %ld hours, %ld minutes and %ld seconds remaining", (long)[components hour], (long)[components minute], (long)[components second]);
    }
}

这篇关于iOS倒计时器到特定日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆