0

我有这个 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];
    }
}

提前致谢。

4

3 回答 3

1

因为currentDate每次您的计时器滴答作响时,您都会继续设置。每次方法运行时[NSDate date]将设置为当前时间。因此总是会提前 45 分钟。您应该创建一个属性并在方法中设置它:currentDateupdateCountdownfinalTimecurrentDatestartDatestart

- (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];
    }
}
于 2013-10-22T06:34:14.737 回答
0

你不需要startDate财产。
而且您不应该检查日期是否相等。这不太可能发生。使用该比较:

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

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

于 2013-10-22T08:52:30.487 回答
0
- (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];
    }
}
于 2013-10-22T09:20:53.927 回答