簡體   English   中英

倒數計時器代碼

[英]Code for countdown timer

我正在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的經驗不是很豐富,我也不知道這應該如何工作。

請幫忙。

這是“ self-> startTime”的非目標符號,但是假設其余代碼有效,則建議如下:

// 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]);

請嘗試以下代碼:

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;

您需要在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];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM