ios - 当发现倒计时结束时

标签 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];
    }
}

最佳答案

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

关于ios - 当发现倒计时结束时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24946824/

相关文章:

ios - 你如何在 Swift 4 中创建文本字段填充?

java - 单击按钮时如何在执行之前取消计时器任务

c++ - 如何在 C++ 中将回调函数指针传递给 epoll_event 结构

android - 如何为recyclerview中的每个列表项添加计数器计时器

java - Android应用程序使用倒计时器在特定时间播放mp3文件

ios - 如何在 Appcelerator 中对按钮/图像产生压抑效果

ios - 是否有 MonoTouch.UIKit.UIKeyboard.BoundsFromNotification 的简单替代品?

ios - 如何在不使用 xcode 的情况下创建类似 .xcappdata 的内容?

c# - Xamarin.Forms 中的 Device.StartTimer 与 System.Threading.Timer

java - Android:当我在另一个 Activity 中时,如何保持倒数计时器运行并在超时时显示一条消息?