我有一个游戏,其中使用倒数计时器,当计时器启动时,你可以看到游戏结束。我想添加一个功能,如果他们点击一个按钮,它会向计时器添加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];
}
答案 0 :(得分:1)
如果您正在使用计时器来管理游戏,同时使用执行选择器(结束游戏或其他任何事情)会使得这一点失败并使管理变得非常复杂。选择一条路线并坚持下去。
当计时器运行时,您可以使用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];
}
删除调用performSelector
的{{1}}后,您的游戏结束定义为
delay
达到零。
顺便说一句,不要这样做:
MainInt
正确的方法是:
if (timer == 0)
如果计时器为零,那么尝试使其无效就没有意义......
另一个好主意是看一下Objective-C naming guidelines。
根据您最近的评论,您似乎确实希望计时器以第二个间隔继续计数,但仅将时间添加到剩余秒数。这更容易,并且不需要更改计时器开火日期。
if (timer == nil)
您需要在'countDownDuration'中添加一个检查:
- (void)extendByTime:(NSInteger)seconds {
MainInt += seconds;
seconds.text = [NSString stringWithFormat:@"%i", MainInt];
}
确定您何时完成。
答案 1 :(得分:0)
您可以继续参考启动计时器的时间。然后,当你想要增加额外的时间时,计算自定时器启动以来已经过了多少时间,使定时器无效并创建一个新的定时器作为时间间隔传递前一个定时器剩余时间与额外秒数之间的差值。
答案 2 :(得分:0)
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
我希望这是有帮助的
答案 3 :(得分:0)
这是一个可以使用的库。