计时器在 45 分钟后不会停止



我有这个NSTimer,我希望它在45分钟后停止,但它不会停止。我做错了什么?

TIMER_COUNT = 45

HOURS_IN_HOURS = 60

HOURS_IN_DAY = 24

- (void)start
{
    self.timerCountdown = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateCountdown) userInfo:nil repeats: YES];
}
- (void)stop
{
    [self.timerCountdown invalidate];
    self.timerCountdown = nil;
}
- (void)updateCountdown
{
    NSDate *currentDate = [NSDate date];
    NSDate *finalTime = [currentDate dateByAddingTimeInterval:(TIMER_COUNT * HOURS_IN_HOUR)];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *componentsHours   = [calendar components:NSHourCalendarUnit fromDate:currentDate];
    NSDateComponents *componentMinuts   = [calendar components:NSMinuteCalendarUnit fromDate:currentDate];
    NSDateComponents *componentSeconds  = [calendar components:NSSecondCalendarUnit fromDate:currentDate];
    NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *componentsDaysDiff = [gregorianCalendar components:NSDayCalendarUnit
                                                                fromDate:currentDate
                                                                  toDate:finalTime
                                                                 options:0];
    NSLog(@"%20d Days, %02d Hours, %02d Minutes, %02d Seconds.", componentsDaysDiff.day, HOURS_IN_DAY - componentsHours.hour, HOURS_IN_HOUR - componentMinuts.minute, HOURS_IN_HOUR - componentSeconds.second);
    if ([currentDate compare:finalTime] == NSOrderedSame)
    {
        NSLog(@"Done.");
        [self stop];
    }
}

提前谢谢。

因为每次计时器滴答作响时,您的currentDate都会不断设置。 每次运行updateCountdown方法时,[NSDate date]都会将currentDate设置为当前时间。因此,finalTime总是比currentDate提前 45 分钟。应创建一个 startDate 属性,并改为在 start 方法中进行设置:

- (void)start
{
    self.timerCountdown = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateCountdown) userInfo:nil repeats: YES];
    self.startDate = [NSDate date];
}

然后在updateCountdown方法中检查属性:

if ([self.startDate compare:finalTime] == NSOrderedSame)
{
    NSLog(@"Done.");
    [self stop];
}

或者,您可以使用具有预期时钟周期数的整数,然后在计时器时钟周期每次从整数中减去一个。像这样:

- (void)start
{
    self.timerCountdown = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateCountdown) userInfo:nil repeats:YES];
    self.countdown = TIMER_COUNT * HOURS_IN_HOUR;
}
- (void)updateCountdown
{
    self.countdown--;
    //your code
    if (self.countdown == 0)
    {
         NSLog(@"Done.");
         [self stop];
    }
}
- (void)start {
   NSString *startTime = [NSDate date];
   NSDateComponents *durationComponents = [[[NSDateComponents alloc] init] autorelease];
   durationComponents.minutes = 45;
   NSCalendar *gregorianCalendar = [NSCalendar currentCalendar];
   self.endTime = [calendar dateByAddingComponents:durationComponents
                                            toDate:startTime
                                           options:0];
   self.timerCountdown = [NSTimer scheduledTimerWithTimeInterval:1.0 
                                                          target:self           
                                                        selector:@selector(updateCountdown)
                                                        userInfo:nil 
                                                         repeats:YES];
}
- (void)stop {
    [self.timerCountdown invalidate];
    self.timerCountdown = nil;
}
- (void)updateCountdown {
    NSDate *currentTime = [NSDate date];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *currentTimeComponents = [calendar components:(NSHourCalendarUnit | NSMinuteCalendarUnit |NSSecondCalendarUnit)
                                                          fromDate:currentTime];

    if ([currentTime compare:self.endTime] == NSOrderedDescending) {
        [self stop];
    }
}

您不需要 startDate 属性。
而且你不应该检查相等的日期。这种情况不太可能发生。使用该比较:

if ([finalTime compare:[NSDate date]] == NSOrderedAscending)
{
     NSLog(@"Done.");
     [self.timerCountdown invalidate];
     [self stop];
}

这意味着 finalTime 的时间早于当前时间。

最新更新