当找出倒计时结束时

时间:2014-07-25 01:50:22

标签: ios timer countdowntimer

目前,我正在运行倒数计时器。我使用的代码如下。

我需要帮助的是找出倒计时结束时间(零时间)。

我无法弄明白。我需要得到它,以便我可以在倒计时结束时发送推送通知。

感谢您的帮助!!

- (IBAction)startCountdown:(id)sender {
    //Remove the time component from the datePicker.  We care only about the date
    NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
    NSUInteger preservedComponents = (NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit);
    self.datePicker.date = [calendar dateFromComponents:[calendar components:preservedComponents fromDate:self.datePicker.date]];

    //Set up a timer that calls the updateTime method every second to update the label
    NSTimer *timer;
    timer = [NSTimer scheduledTimerWithTimeInterval:1.0
                                             target:self
                                           selector:@selector(updateTime)
                                           userInfo:nil
                                            repeats:YES];
}

-(void)updateTime
{
    //Get the time left until the specified date
    NSInteger ti = ((NSInteger)[self.datePicker.date timeIntervalSinceNow]);
    NSInteger seconds = ti % 60;
    NSInteger minutes = (ti / 60) % 60;
    NSInteger hours = (ti / 3600) % 24;
    NSInteger days = (ti / 86400);

    //Update the lable with the remaining time
    self.countdownLabel.text = [NSString stringWithFormat:@"%02i days %02i hrs %02i min %02i sec", days, hours, minutes, seconds];
}

更新 这是我到目前为止所尝试的:

- (void) bothDatesEqual {

    if ([_countdownLabel.text isEqualToString:@"00 days 00 hrs 00 min 00 sec"]) {
        NSLog(@"No time left");
    }
    else {
        NSLog(@"Some time left");
    }
}

更新2 这是我根据DanH改变的内容

- (void)startCountdownDate {

    //Remove the time component from the datePicker.  We care only about the date
    NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
    NSUInteger preservedComponents = (NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit);
    self.datePicker.date = [calendar dateFromComponents:[calendar components:preservedComponents fromDate:self.datePicker.date]];

    [self updateTime];

    NSTimer *timerDate;
    timerDate  = [NSTimer scheduledTimerWithTimeInterval:1.0
                                             target:self
                                                selector:@selector(updateTimeDate:)
                                           userInfo:nil
                                            repeats:YES];

}

-(void)updateTime
{
    //Get the time left until the specified date
    NSInteger ti = ((NSInteger)[self.datePicker.date timeIntervalSinceNow]);
    NSInteger seconds = ti % 60;
    NSInteger minutes = (ti / 60) % 60;
    NSInteger hours = (ti / 3600) % 24;
    NSInteger days = (ti / 86400);

    //Update the label with the remaining time
    _countdownLabel.text = [NSString stringWithFormat:@"%02i days %02i hrs %02i min %02i sec", days, hours, minutes, seconds];
}

- (void)updateTimeDate:(NSTimer *)timerDate {

    NSDate *now = [NSDate date];
    NSDate *targetDate = self.datePicker.date;

    if (now == [now laterDate:targetDate]) {
        // the current time is >= targetDate
        [timerDate invalidate];
    }
}

1 个答案:

答案 0 :(得分:1)

使用NSTimer的更好方法是将其视为脉冲,而不是时间源。正确的时间来源是[NSDate date]。因此,继续进行的方法是设置计时器,在计时器触发时更改UI,并测试:

// updateTime
// give the selector a colon when you schedule the timer and when you implement it
// that will give you access to the timer

-(void)updateTime:(NSTimer *)timer {

    NSDate *now = [NSDate date];
    NSDate *targetDate = self.datePicker.date;

    if (now == [now laterDate:targetDate]) {
        // the current time is >= targetDate
        [timer invalidate];
    }

    // the rest of your updateTime logic here, but see below...

请记住,在安排时要更改给选择器一个参数(冒号)......

selector:@selector(updateTime:)

另外,Here's a good SO answer提供了一个更加可靠的方法,用于从NSTimeInterval中提取时间组件。

编辑 - 关于时间的更好答案被埋没在那个问题中(不是标记为正确的答案或最多的投票)。本质上......

NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [gregorianCalendar components: (NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit )
                                                    fromDate:targetDate
                                                      toDate:now
                                                     options:0];
NSLog(@"%ld", [components year]);
NSLog(@"%ld", [components month]);
NSLog(@"%ld", [components day]);
NSLog(@"%ld", [components hour]);
NSLog(@"%ld", [components minute]);
NSLog(@"%ld", [components second]);