每次运行时减少 NSTimer 间隔

发布于 2024-12-11 22:05:16 字数 402 浏览 0 评论 0原文

我希望我的 NSTimer 每次运行时都能加速:

-(void)runtimer {
   int delay = delay - 1;
   [NSTimer scheduledTimerWithTimeInterval:(delay) 
                                    target:self 
                                  selector:@selector(timerTriggered:) 
                                  userInfo:nil 
                                   repeats:YES];

}

但这不起作用。怎样才能让延迟越来越小呢?

I want my NSTimer to speed up each time it's run:

-(void)runtimer {
   int delay = delay - 1;
   [NSTimer scheduledTimerWithTimeInterval:(delay) 
                                    target:self 
                                  selector:@selector(timerTriggered:) 
                                  userInfo:nil 
                                   repeats:YES];

}

But this doesn't work. How can I make the delay keep getting smaller and smaller?

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

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

发布评论

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

评论(4

城歌 2024-12-18 22:05:16

自己需要这个并编写了一个组件CPAccelerationTimer (Github)

[[CPAccelerationTimer accelerationTimerWithTicks:20
    totalDuration:10.0
    controlPoint1:CGPointMake(0.5, 0.0) // ease in
    controlPoint2:CGPointMake(1.0, 1.0)
    atEachTickDo:^(NSUInteger tickIndex) {
        [self timerTriggered:nil];
    } completion:^{
        [self timerTriggered:nil];
    }]
run];

这会调用 -timerTriggered: 20 次,分布在 10 秒内,其中不断减少的延迟(由给定的贝塞尔曲线指定)。

I needed this myself and wrote a component CPAccelerationTimer (Github):

[[CPAccelerationTimer accelerationTimerWithTicks:20
    totalDuration:10.0
    controlPoint1:CGPointMake(0.5, 0.0) // ease in
    controlPoint2:CGPointMake(1.0, 1.0)
    atEachTickDo:^(NSUInteger tickIndex) {
        [self timerTriggered:nil];
    } completion:^{
        [self timerTriggered:nil];
    }]
run];

This calls -timerTriggered: 20 times, spread out over 10 seconds, with ever-decreasing delays (as specified by the given Bézier curve).

苦行僧 2024-12-18 22:05:16

每次运行此方法时,您都会创建一个名为 delay 的新变量,然后尝试将其设置为自身减 1。这是未定义的行为(该变量未初始化为任何内容),并且很可能导致延迟的垃圾值。*

您需要将延迟存储在实例变量中。

- (void) runTimer {
    // You are declaring a new int called |delay| here.
    int delay = delay - 1;
    // This is not the same |delay| that you have declared in your header.
    // To access that variable, use:
    delay = delay - 1;

*邪恶超自然生物感染鼻窦也是一种可能。

Every time this method is run, you make a new variable called delay, then try to set it to itself minus 1. This is Undefined Behavior (the variable was not initialized to anything), and is likely to result in a garbage value for delay.*

You need to store the delay in an instance variable.

- (void) runTimer {
    // You are declaring a new int called |delay| here.
    int delay = delay - 1;
    // This is not the same |delay| that you have declared in your header.
    // To access that variable, use:
    delay = delay - 1;

*A sinus infestation by evil-aligned supernatural beings is also a possibility.

蝶…霜飞 2024-12-18 22:05:16

您必须在某个地方声明延迟,例如在类接口中或作为静态变量。

另外,每次都创建一个新的计时器,而不是让它重复。

int delay = INITIAL_DELAY;

-(void)runtimer{
    [NSTimer scheduledTimerWithTimeInterval:(NSTimeInterval)(delay--) target:self selector:@selector(runTimer:) userInfo:nil repeats:NO];
}

You have to declare the delay somewhere, like in the class interface or as a static variable.

Also, create a new timer every time, instead of having it repeat.

int delay = INITIAL_DELAY;

-(void)runtimer{
    [NSTimer scheduledTimerWithTimeInterval:(NSTimeInterval)(delay--) target:self selector:@selector(runTimer:) userInfo:nil repeats:NO];
}
姐不稀罕 2024-12-18 22:05:16

创建计时器后,您将无法更改其触发间隔。如果您想要不同的间隔,则必须使先前的计时器无效(因此您应该保留对它的引用),并创建一个具有不同间隔的新计时器。

You cannot change the fire interval of the timer once you have created it. If you want a different interval you must invalidate the previous timer (hence you should keep a reference to it), and create a new timer with a different interval.

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