倒数计时器的代码

时间:2013-12-11 20:31:24

标签: ios objective-c nsdate

我正在Xcode中创建一个倒数计时器。用户设置小时,分钟和秒,然后应用程序倒计时。但是,我的代码遇到了一些麻烦,我无法弄清楚要做什么。这是代码:

elapsedTime = [[NSDate date] timeIntervalSinceDate:self->startTime];

int elapsed = (int)elapsedTime - (int)totalStoppedTime;

secondInt = setSecond - (elapsed % 60);
if (secondInt < 0) {
    secondInt = 0;
}
minuteInt = setMinute - ((elapsed / 60) % 60);
if (minuteInt < 0) {
    minuteInt = 0;
    }
hourInt = setHour - (elapsed / 3600);
if (hourInt < 0) {
    hourInt = 0;
}

基本上,我在用户启动计时器时创建一个NSDate,并且每0.5秒调用一次此方法。它计算自开始时间以来经过的时间(取消总停止时间 - 用户停止计时器所花费的时间),并将其从计时器中取出。前两行工作正常。我遇到的麻烦是带走了经过的时间。

SetSecond / SetMinute / SetHour是保存用户为应用倒计时设置的初始秒,分,小时数的变量。 SecondInt / MinuteInt / HourInt是在显示之前保留时间的变量。

当您将分钟和小时设置为0时,会出现此问题。直到自开始时间过去60秒后才会更新。因此,如果您将分钟设置为5,将秒设置为20,则分钟在20秒后不会改变,而是在60秒后改变。

我试图尽我所能解释这一点,但如果您需要更多信息,请询问。

感谢您的帮助!

编辑:这是我现在的代码:

NSDate *now = [NSDate date];

NSUInteger flags = NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *components = [[NSCalendar currentCalendar] components:flags fromDate:self->startTime toDate:now options:0];

secondInt = [components second];
minuteInt = [components minute];
hourInt = [components hour];

NSTimeInterval totalElapsed = [now timeIntervalSinceDate:self->startTime];
NSTimeInterval netElapsed = totalElapsed - totalStoppedTime;

NSDate *sinceStart = [NSDate dateWithTimeInterval:netElapsed sinceDate:self->startTime];

我收到一条警告,上面写着'未使用的变量:sinceStart'。我真的完全不知道我在做什么。我对Objective-C不是很有经验,我不知道它应该如何工作。

请帮忙。

3 个答案:

答案 0 :(得分:0)

这是“self-gt; startTime”的非客观c符号,但假设其余代码有效,这里有一个建议:

// start the timer, self.startDate = [NSDate date];

然后在你的计时器触发方法......

NSDate *now = [NSDate date]; 
NSTimeInterval totalElapsed = [now timeIntervalSinceDate:self.startDate];
NSTimeInterval netElapsed = totalElapsed - totalStoppedTime;

NSDate *sinceStart = [NSDate dateWithTimeInterval:netElapsed sinceDate:self.startDate];

// now use "sinceStart" in the NSCalendar components call, like this...
NSDateComponents *components = [[NSCalendar currentCalendar] components:flags fromDate:sinceStart toDate:now options:0];

NSInteger secondInt = [components second];
NSInteger minuteInt = [components minute];
NSInteger hourInt = [components hour];

// use these values in your UI
NSLog(@"there are %d hours, %d minutes and %d seconds remaining", [components hour], [components minute], [components second]);

答案 1 :(得分:0)

请尝试以下代码:

NSTimeInterval duration = setSecond + setMinute * 60 + setMinute * 3600;
NSDate *endDate = [NSDate dateWithTimeInterval:duration sinceDate:self->startDate];
NSTimeInterval remainingTime = [endDate timeIntervalSinceNow];
if(remainingTime <= 0)
    NSLog(@"Finish");

int remainingInSeconds = (int) remainingTime;

secondInt = remainingInSeconds % 60;
remainingInSeconds /= 60;

minuteInt = remainingInSeconds % 60;
remainingInSeconds /= 60;

hourInt = remainingInSeconds % 24;

答案 2 :(得分:0)

您需要在NSCalander调用中使用计算结果。

NSDate *now = [NSDate date];

NSTimeInterval totalElapsed = [now timeIntervalSinceDate:startTime];
NSTimeInterval netElapsed = totalElapsed - totalStoppedTime;

NSDate *sinceStart = [NSDate dateWithTimeInterval:netElapsed sinceDate:startTime];

NSUInteger flags = NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *components = [[NSCalendar currentCalendar] components:flags fromDate:sinceStart toDate:now options:0];

secondInt = [components second];
minuteInt = [components minute];
hourInt = [components hour];