代码之家  ›  专栏  ›  技术社区  ›  Realinstomp

当倒数计时结束时

  •  0
  • Realinstomp  · 技术社区  · 12 年前

    目前,我有一个 倒数计时器 跑步我使用的代码如下。

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

    我不太明白。我需要得到它,以便在倒计时结束时发送推送通知。

    谢谢你的帮助!!

    - (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 回复  |  直到 12 年前
        1
  •  1
  •   Community Mohan Dere    9 年前

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