[英]How to add time to a countdown timer to iOS Project
我有一個使用倒數計時器的游戲,當計時器計時到時,您就會迷上游戲。 我想添加一個功能,如果他們點擊一個按鈕,它將為計時器增加1、2或3秒。 我已經有了計時器的代碼(下),但是我只需要知道如何增加計數器的時間即可。 我想到了這一點,我不得不說什么時候視圖將切換,並且需要考慮增加的時間。
碼:
-(IBAction)Ready:(id)sender {
[self performSelector:@selector(TimerDelay) withObject:nil afterDelay:1.0];
[self performSelector:@selector(delay) withObject:nil afterDelay:36.5];
}
-(void)TimerDelay {
MainInt = 36;
timer = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:@selector(countDownDuration)
userInfo:nil
repeats:YES];
if (timer == 0)
{
[timer invalidate];
}
}
-(void)countDownDuration {
MainInt -= 1;
seconds.text = [NSString stringWithFormat:@"%i", MainInt];
}
-(void)delay {
GameOverView1_4inch *second= [self.storyboard instantiateViewControllerWithIdentifier:@"GameOverView1"];
second.finalScore = self.currentScore;
[self presentViewController:second animated:YES completion:nil];
}
如果您使用計時器來管理游戲,則同時使用執行選擇器(以結束游戲或其他任何事情)會破壞這一點,並使管理變得非常復雜。 選擇一種路線並堅持下去。
當計時器運行時,您可以使用setFireDate:
更改其觸發日期。 因此,您可以獲取當前的開始日期,為其添加時間,然后設置新的開始日期:
- (void)extendByTime:(NSInteger)seconds
{
NSDate *newFireDate = [[self.timer fireDate] dateByAddingTimeInterval:seconds];
[self.timer setFireDate:newFireDate];
}
然后,您的按鈕回調類似於:
- (void)buttonOnePressed:(id)sender
{
[self extendByTime:1];
}
- (void)buttonFivePressed:(id)sender
{
[self extendByTime:5];
}
一旦您刪除了調用delay
的performSelector
,您的游戲結束將由MainInt
達到零來定義。
順便說一句,不要這樣做:
if (timer == 0)
正確的方法是:
if (timer == nil)
而且如果計時器為零,那么嘗試使其無效是沒有意義的...
看看Objective-C 命名准則也是一個好主意。
根據您最近的評論,您似乎實際上希望計時器以第二個間隔繼續計數,但只將時間添加到剩余的秒數中。 這甚至更容易,並且不需要更改計時器啟動日期。
- (void)extendByTime:(NSInteger)seconds {
MainInt += seconds;
seconds.text = [NSString stringWithFormat:@"%i", MainInt];
}
並且您需要在“ countDownDuration”中添加檢查:
if (MainInt <= 0) {
[timer invalidate];
[self delay];
}
確定何時完成。
您可以參考啟動計時器的時間。 然后,當您想增加額外的時間時,計算自計時器啟動以來已過去了多少時間,使計時器無效並創建一個新的計時器作為時間間隔,該時間間隔是前一個計時器剩余時間與額外秒數之間的差。
Hy,請嘗試使用此方法:
把它放在-(void)viewDidLoad方法中
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self
selector:@selector(countDownTimer) userInfo:nil repeats:YES];
然后創建-(void)countDownTimer方法
- (void)countDownTimer {
// my method which returns the differences between two dates in my case
double diff = [self getDateDifference];
double days = trunc(diff / (60 * 60 * 24));
double seconds = fmod(diff, 60.0);
double minutes = fmod(trunc(diff / 60.0), 60.0);
double hours = fmodf(trunc(diff / 3600.0), 24);
if(diff > 0) {
NSString *countDownString = [NSString stringWithFormat:@"%02.0f day(s)\n%02.0f:%02.0f:%02.0f",
days, hours, minutes, seconds];
// IBOutlet label, added in .h
self.labelCountDown.text= countDownString;
} else {
// stoping the timer
[timer invalidate];
timer = nil;
// do something after countdown ...
}
}
並且您可以添加-(double)getDateDifference方法,該方法返回我的情況下兩個日期之間的差
- (double)getDateDifference {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSDate *dateFromString = [[NSDate alloc] init];
NSDate *now = [NSDate date];
NSString *myDateString = @"2013-10-10 10:10:10"; // my initial date with time
// if you want to use only time, than delete the
// date in myDateString and setDateFormat:@"HH:mm:ss"
// this line is not required, I used it, because I need GMT+2
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:+2]];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
// get the date
dateFromString = [dateFormatter dateFromString:myDateString];
// this line is also not required, I used it because I need GMT+2
// so I added two hours in seconds to 'now'
now = [now dateByAddingTimeInterval:60*60*2];
// getting the difference
double diff = [dateFromString timeIntervalSinceDate:now];
NSLog(@"dateString: %@", dateString);
NSLog(@"now: %@", now);
NSLog(@"targetDate: %@", dateFromString);
NSLog(@"diff: %f", diff);
return diff;
}
輸出與此類似
dateString: 2013-10-10 00:20:00
now: 2013-10-10 00:20:00 +0000
target: 2013-10-10 00:20:28 +0000
diff: 28.382786
希望對您有所幫助
這是您可以使用的庫。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.