如何将时间添加到iOS项目的倒数计时器

我有一个游戏在哪里使用倒数计时器,当计时器到时,你是一个博弈观点。 我想添加一个function,如果他们点击一个button,它会增加像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]; } 

如果您使用计时器来pipe理游戏,同时使用执行select器(结束游戏或其他任何事情)会使这一点失败,并使pipe理变得非常复杂。 select一条路线并坚持下去。

定时器运行时,可以使用setFireDate:来更改它的启动date。 所以,你可以得到当前的火灾date,增加你的时间,然后设置新的火灾date:

 - (void)extendByTime:(NSInteger)seconds { NSDate *newFireDate = [[self.timer fireDate] dateByAddingTimeInterval:seconds]; [self.timer setFireDate:newFireDate]; } 

然后,你的buttoncallback是这样的:

 - (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 命名指南也是一个好主意。


根据您最近的评论,您似乎确实希望定时器在第二个时间间隔内继续计数,但只能将时间增加到剩余的秒数。 这更简单,不需要对定时器的启动date进行任何更改。

 - (void)extendByTime:(NSInteger)seconds { MainInt += seconds; seconds.text = [NSString stringWithFormat:@"%i", MainInt]; } 

你需要在“countDownDuration”中添加一个检查:

 if (MainInt <= 0) { [timer invalidate]; [self delay]; } 

确定何时完成。

您可以继续参考您启动计时器的时间。 然后,当你想增加额外的时间,计算自定时器启动以来已经过了多less时间,使定时器无效,并创build一个新的时间间隔作为前一个定时器的剩余时间和额外的秒数之间的差异。

Hy,试着用这个:

把它放在– (void)viewDidLoad方法中

 NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(countDownTimer) userInfo:nil repeats:YES]; 

然后创build– (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方法,返回两个date之间的差异在我的情况

 - (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 

我希望这是有帮助的

这是一个你可以使用的库。

https://github.com/akmarinov/AMClock