如何更改 NSTimer 的计时?

发布于 2024-10-01 08:19:39 字数 474 浏览 5 评论 0原文

我有以下代码:

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

-(void) onTimer 
{
}

每 0.50 秒调用一次 OnTimer 方法。

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

这意味着:

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.

有什么解决办法吗?我已经尝试了很多,但它不起作用。

I have the following code:

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

-(void) onTimer 
{
}

After every 0.50 seconds the OnTimer method is called.

But now I want to increment the time-interval.

That means:

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.

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

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

发布评论

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

评论(5

带上头具痛哭 2024-10-08 08:19:39

当然你可以做到这一点。将 repeats:YES 更改为 repeats:NO 以便计时器不重复,然后在 onTimer 中,只需使用间隔较长。您需要一个变量来保存间隔,以便每次通过 onTimer 可以将间隔延长一点。另外,您可能不再需要保留计时器,因为它只会触发一次,当它触发时,您将获得一个新的计时器。

我不是 Objective-C 专家(或 iOS 专家...),虽然已经有一段时间了,但我想的是这样的:

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 语义...阅读文档以确保不会泄漏!

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

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!

孤凫 2024-10-08 08:19:39

这是使用 NSTimer 的 fireDate 属性(用 Swift 编写)解决 Hardik 问题的一种解决方案:

Swift 2

var timeInterval = 0.50
NSTimer.scheduledTimerWithTimeInterval(timeInterval, target: self, selector: Selector("onTimer:"), userInfo: nil, repeats: true)

func onTimer(timer: NSTimer) {
    timeInterval += 0.05
    timer.fireDate = timer.fireDate.dateByAddingTimeInterval(timeInterval)
}

Swift 3, 4, 5

var timeInterval = 0.50
Timer.scheduledTimer(timeInterval: timeInterval, target: self, selector: #selector(self.onTimer), userInfo: nil, repeats: true)

@objc func onTimer(timer: Timer) {
    timeInterval += 0.05
    timer.fireDate = timer.fireDate.addingTimeInterval(timeInterval)
}

Here is one solution to Hardik's question using NSTimer's fireDate property (written in Swift):

Swift 2

var timeInterval = 0.50
NSTimer.scheduledTimerWithTimeInterval(timeInterval, target: self, selector: Selector("onTimer:"), userInfo: nil, repeats: true)

func onTimer(timer: NSTimer) {
    timeInterval += 0.05
    timer.fireDate = timer.fireDate.dateByAddingTimeInterval(timeInterval)
}

Swift 3, 4, 5

var timeInterval = 0.50
Timer.scheduledTimer(timeInterval: timeInterval, target: self, selector: #selector(self.onTimer), userInfo: nil, repeats: true)

@objc func onTimer(timer: Timer) {
    timeInterval += 0.05
    timer.fireDate = timer.fireDate.addingTimeInterval(timeInterval)
}
老子叫无熙 2024-10-08 08:19:39

您可以尝试调整计时器的 FireDate,请参阅 setFireDate

You could try adjusting the timer's FireDate, see setFireDate

本宫微胖 2024-10-08 08:19:39

您无法更改计时器触发的时间间隔。决不。没有 API 可以执行此操作。

两个明显的解决方法:如果您总是更改间隔,那么您可以更改计时器下次触发的时间;这优先于计时器间隔。当然,每次计时器触发时您都必须这样做。如果您有时更改间隔,只需使第一个计时器无效并使用新间隔创建一个新计时器即可。

You can't change the interval at which a timer fires. No way. There's no API for doing this.

Two obvious workarounds: If you always change the interval, then you can just change the next time that the timer fires; this has precedence over the timer interval. Of course you have to do that every time the timer fires. If you sometimes change the interval, just invalidate the first timer and create a new timer with the new interval.

迷荒 2024-10-08 08:19:39

这对我来说就像魅力一样。

- (void)FlexibleTimer {
    if (!keyTimer) {
        keyTimer = [NSTimer scheduledTimerWithTimeInterval:self.intervalOfIdleTimer
                                                      target:self
                                                    selector:@selector(keyTimerExceeded)
                                                    userInfo:nil
                                                     repeats:YES];
    }
    else {
        if (fabs([keyTimer.fireDate timeIntervalSinceNow]) < self.intervalOfIdleTimer - 1.0) {
            [keyTimer setFireDate:[NSDate dateWithTimeIntervalSinceNow:self.intervalOfIdleTimer]];
        }
    }
}

- (void)keyTimerExceeded {
    [self setNextDelayForFlexibleTimer];
    [self FlexibleTimer];
    NSLog(@"Timer Exceeded %f", [[NSDate date] timeIntervalSince1970]);
}

This just work like charm for me.

- (void)FlexibleTimer {
    if (!keyTimer) {
        keyTimer = [NSTimer scheduledTimerWithTimeInterval:self.intervalOfIdleTimer
                                                      target:self
                                                    selector:@selector(keyTimerExceeded)
                                                    userInfo:nil
                                                     repeats:YES];
    }
    else {
        if (fabs([keyTimer.fireDate timeIntervalSinceNow]) < self.intervalOfIdleTimer - 1.0) {
            [keyTimer setFireDate:[NSDate dateWithTimeIntervalSinceNow:self.intervalOfIdleTimer]];
        }
    }
}

- (void)keyTimerExceeded {
    [self setNextDelayForFlexibleTimer];
    [self FlexibleTimer];
    NSLog(@"Timer Exceeded %f", [[NSDate date] timeIntervalSince1970]);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文