如何更改NSTimer的时间? [英] How do I change timing for NSTimer?

查看:117
本文介绍了如何更改NSTimer的时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

timer = [[NSTimer scheduledTimerWithTimeInterval:0.50 target:self selector:@selector(onTimer) userInfo:nil repeats:YES] retain];    

-(void) onTimer 
{
}

每0.50秒后调用 OnTimer 方法。

After every 0.50 seconds the OnTimer method is called.

但现在我想增加时间间隔。

But now I want to increment the time-interval.

这意味着:

OnTimer calls after 0.55
OnTimer calls after 0.60
OnTimer calls after 0.65
OnTimer calls after 0.70
OnTimer calls after 0.75
& so on.

有没有解决方案?我已经尝试了很多,但它不起作用。

Is there any solution for this?? I have tried lot but its not working.

推荐答案

当然你可以做到这一点。更改重复:是重复:否以便计时器不重复,然后在<$ c $中c> onTimer ,只需启动一个间隔较长的新计时器。你需要一个变量来保持你的间隔,这样你每次通过 onTimer 可以让它变得更长。此外,您可能不再需要保留计时器,因为它只会触发一次,当它发生时,您将获得一个新计时器。

Sure you can do this. Change repeats:YES to repeats:NO so that the timer doesn't repeat, and then in onTimer, just start a new timer with a longer interval. You need a variable to hold your interval so that you can make it a bit longer each time through onTimer. Also, you probably don't need to retain the timer anymore, as it will only fire once, and when it does, you'll get a new timer.

I我没有Objective-C专家(或iOS专家......)而且已经有一段时间了,但我认为是这样的:

I'm no Objective-C expert (or iOS expert...) and it's been a while but I think something like this:

float gap = 0.50;

[NSTimer scheduledTimerWithTimeInterval:gap target:self selector:@selector(onTimer) userInfo:nil repeats:NO];

-(void) onTimer {
    gap = gap + .05;
    [NSTimer scheduledTimerWithTimeInterval:gap target:self selector:@selector(onTimer) userInfo:nil repeats:NO];
}

这样的东西?哦,我真的不太确定这里的 retain 语义...阅读文档以确保你没有泄漏!

Something like that? Oh, and I'm really not too sure about the retain semantics here... read the docs to make sure you don't leak!

这篇关于如何更改NSTimer的时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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