如何为 iOS 项目的倒数计时器添加时间 [英] How to add time to a countdown timer to iOS Project

查看:39
本文介绍了如何为 iOS 项目的倒数计时器添加时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用倒数计时器的游戏,当计时器到时,您会看到游戏概览.我想添加一个功能,如果他们点击一个按钮,它会为计时器增加 1、2 或 3 秒.我已经有了计时器的代码(如下),但我只需要知道如何为计数器添加更多时间.我想到了,我不得不说视图何时切换,需要计算增加的​​时间.

I have a game where is uses a countdown timer and when the timer is up, you are brougt to a Game over view. I want to add a feature were if they tap a button, it will add like 1, 2 or 3 more seconds to the timer. I already have the code for the timer (Below), but i just need to know how to add more time to the counter. I thought of it and i have to say when the views will switch and it would need to take into a count the added time.

代码:

-(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];
}

推荐答案

如果您使用计时器来管理游戏,那么同时使用执行选择器(以结束游戏或其他任何事情)有点失败点,使管理变得非常复杂.选择一条路线并坚持下去.

If you're using a timer to manage the game, using a perform selector at the same time (to end the game or anything else) kind of defeats the point and makes the management very complex. Choose one route and stick with it.

当计时器运行时,您可以使用 setFireDate: 更改其触发日期.因此,您可以获取当前的开火日期,添加您的时间,然后设置新的开火日期:

When the timer is running, you can change it's fire date using setFireDate:. So, you could get the current fire date, add your time to it and then set the new fire date:

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

然后,您的按钮回调类似于:

Then, your button callbacks are something like:

- (void)buttonOnePressed:(id)sender
{
    [self extendByTime:1];
}

- (void)buttonFivePressed:(id)sender
{
    [self extendByTime:5];
}

一旦您删除了调用 delayperformSelector,您的游戏结束将被定义为MainInt 达到零.

Once you've removed the performSelector which calls delay your game end will be defined by MainInt reaching zero.

顺便说一句,不要这样做:

As an aside, don't do this:

if (timer == 0)

正确的做法是:

if (timer == nil)

如果计时器为零,则尝试使其无效...

And if the timer is nil, there's no point in trying to invalidate it...

看看 Objective-C 命名指南.

Also a good idea to take a look at the Objective-C naming guidelines.

根据您最近的评论,您似乎实际上希望计时器以第二个间隔继续计数,但仅将时间添加到剩余的秒数.这更容易,而且不需要对计时器触发日期进行任何更改.

Based on your recent comment, it seems that you actually want the timer to continue counting at a second interval, but to add time only to the number of seconds remaining. That's even easier and doesn't require any change to the timer fire date.

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

并且您需要在countDownDuration"中添加一个检查:

And you need to add a check in 'countDownDuration':

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

确定何时完成.

这篇关于如何为 iOS 项目的倒数计时器添加时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆