增加定时器的时间间隔

发布于 2024-12-08 19:13:54 字数 448 浏览 4 评论 0原文

可能的重复:
更改计时器的时间间隔

所以我有一个计时器:

timer=[NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(createNewImage) userInfo:nil repeats:YES];

并且我希望计时器每十秒减少一次,scheduledTimerWithTimeInterval 在十秒后变为 1.5,然后在 10 秒后变为 1.0...是否可以做到这一点,如果可能的话,我该怎么做?

Possible Duplicate:
Change the time interval of a Timer

So I have a timer:

timer=[NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(createNewImage) userInfo:nil repeats:YES];

And I would like that the timer decrease every ten seconds, that scheduledTimerWithTimeInterval goes to 1.5 after ten seconds then to 1.0 after 10 seconds... Is it possible to do this, and if it is possible, how can I do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

濫情▎り 2024-12-15 19:13:54

创建计时器后就无法对其进行修改。要更改计时器间隔,请使旧计时器无效并使用新间隔创建一个新计时器。

You can't modify a timer after you created it. To change the timer interval, invalidate the old timer and create a new one with the new interval.

远昼 2024-12-15 19:13:54

您不必使用多个计时器,您所要做的就是添加一个用于时间间隔的变量,然后创建一个使计时器无效、更改变量并再次启动计时器的方法。例如,您可以创建一个启动计时器的方法。

int iTimerInterval = 2;

-(void) mTimerStart {
    timer = [NSTimer scheduledTimerWithTimeInterval:iTimerInterval target:self selector:@selector(createNewImage) userInfo:nil repeats:YES];
}

-(void) mTimerStop {
    [timer invalidate];

    iTimerInterval = iTimerInterval + 5;

    [self mTimerStart];
}

这将是减少计时器间隔并保持计时器运行的简单方法,但我个人更喜欢使用下面的方法,因为它确保计时器只运行一次,这样它就不会重复实例,从而迫使您应用程序变得故障,它也让你的事情变得更容易。

int iTimerInterval = 2;
int iTimerIncrementAmount = 5;
int iTimerCount;
int iTimerChange = 10; //Variable to change the timer after the amount of time
bool bTimerRunning = FALSE;

-(void) mTimerToggle:(BOOL)bTimerShouldRun {
    if (bTimerShouldRun == TRUE) {
        if (bTimerRunning == FALSE) {
            timer = [NSTimer scheduledTimerWithTimeInterval:iTimerInterval target:self selector:@selector(mCreateNewImage) userInfo:nil repeats:YES];
            bTimerRunning = TRUE;
        }
    } else if (bTimerShouldRun == FALSE) {
        if (bTimerRunning == TRUE) {
            [timer invalidate];
            bTimerRunning = FALSE;
        }
    }
}

-(void) mCreateNewImage {
    //Your Code Goes Here

    if (iTimerCount % iTimerChange == 0) { //10 Second Increments
        iTimerInterval = iTimerInterval + iTimerIncrementAmount; //Increments by Variable's amount

        [self mTimerToggle:FALSE]; //Stops Timer
        [self mTimerToggle:TRUE]; //Starts Timer
    }

    iTimerCount ++;
}

-(void) mYourMethodThatStartsTimer {
    [self mTimerToggle:TRUE];
}

我没有完成所有的编码,但这就是您需要的大部分内容。只需更改一些内容即可开始使用!

You don't have to use several timers, all you have to do is add a variable to use for the time interval, then create a method that invalidates the timer, changes the variable and starts the timer again. For instance you could make a method that starts the timer.

int iTimerInterval = 2;

-(void) mTimerStart {
    timer = [NSTimer scheduledTimerWithTimeInterval:iTimerInterval target:self selector:@selector(createNewImage) userInfo:nil repeats:YES];
}

-(void) mTimerStop {
    [timer invalidate];

    iTimerInterval = iTimerInterval + 5;

    [self mTimerStart];
}

This would be the simple way to decrease the timer interval and keep the timer going, but I would personally prefer using the one below, because it makes sure that the timer has only ran once, that way it will not duplicate the instance, forcing your app to become glitchy and it also makes things easier for you.

int iTimerInterval = 2;
int iTimerIncrementAmount = 5;
int iTimerCount;
int iTimerChange = 10; //Variable to change the timer after the amount of time
bool bTimerRunning = FALSE;

-(void) mTimerToggle:(BOOL)bTimerShouldRun {
    if (bTimerShouldRun == TRUE) {
        if (bTimerRunning == FALSE) {
            timer = [NSTimer scheduledTimerWithTimeInterval:iTimerInterval target:self selector:@selector(mCreateNewImage) userInfo:nil repeats:YES];
            bTimerRunning = TRUE;
        }
    } else if (bTimerShouldRun == FALSE) {
        if (bTimerRunning == TRUE) {
            [timer invalidate];
            bTimerRunning = FALSE;
        }
    }
}

-(void) mCreateNewImage {
    //Your Code Goes Here

    if (iTimerCount % iTimerChange == 0) { //10 Second Increments
        iTimerInterval = iTimerInterval + iTimerIncrementAmount; //Increments by Variable's amount

        [self mTimerToggle:FALSE]; //Stops Timer
        [self mTimerToggle:TRUE]; //Starts Timer
    }

    iTimerCount ++;
}

-(void) mYourMethodThatStartsTimer {
    [self mTimerToggle:TRUE];
}

I didn't finish all of the coding, but this is most of what you will need. Just change a few things and you'll be good to go!

靑春怀旧 2024-12-15 19:13:54

您将需要一组或一系列计时器,每个计时器对应不同的时间间隔。当您想要更改为时间序列中的下一个时间间隔时,停止/无效一个计时器并启动下一个计时器。

You will need a set or sequence of timers, one for each different time interval. Stop/invalidate one timer and start the next timer when you want to change to the next time interval in your time sequence.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文