(iPhone)重复时我是否需要使计时器无效:不需要?

发布于 2024-10-20 06:33:20 字数 302 浏览 5 评论 0原文

[NSTimer scheduledTimerWithTimeInterval: target: selector: userInfo: repeats:NO];

当repeats:设置为NO时,我是否需要使指定选择器内的计时器无效?

谢谢

编辑

另一个问题,如果它自行失效,

如何正确取消这样的计时器?
因为使已经无效的计时器无效我认为会崩溃?

维护一个指向计时器的指针并将其设置为 nil 在将被触发的选择器中?

[NSTimer scheduledTimerWithTimeInterval: target: selector: userInfo: repeats:NO];

When repeats: is set to NO, do I need to invalidate the timer inside the specified selector?

Thank you

Edit

Another question, if it self invalidates,

How do you properly cancel a such timer?
Since invalidating already-invalidated timer would crash I assume?

maintain a pointer to the timer and set it to nil inside the selector that will get fired?

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

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

发布评论

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

评论(5

香橙ぽ 2024-10-27 06:33:20

不,计时器会自行失效

No, the timer will invalidate itself

梦毁影碎の 2024-10-27 06:33:20

@Eugene,如果您在选择器方法中使用

[NSTimer scheduledTimerWithTimeInterval: target: selector: userInfo: repeats:YES];

then ,您需要提供一个像这样的函数

- (void)timerFireMethod:(NSTimer*)theTimer

,这样当您想要使其无效时,您可以有一个像这样的条件

if(workDone == YES)
{
   [theTimer invalidate];
}

但是如果您在重复选项,则计时器将自行失效。

@Eugene if you are using

[NSTimer scheduledTimerWithTimeInterval: target: selector: userInfo: repeats:YES];

then in the selector method you need to give a function like this one

- (void)timerFireMethod:(NSTimer*)theTimer

so when you want to invalidate it you can have a condition like this one

if(workDone == YES)
{
   [theTimer invalidate];
}

But if you are using NO in the repeat option then the timer will invalidate itself.

回忆凄美了谁 2024-10-27 06:33:20

您可以维护标志来保存计时器是否已被触发。

例如。

    BOOL gameOver = NO;
    NSTimer * gameOverTimer;


-(void)startGame
{

     gameOverTimer =    [NSTimer scheduledTimerWithTimeInterval:600 target:self selector:@selector(stopLevel:) userInfo:nil repeats:NO]; 
     // your code
}

-(void)stopLevel:(id)sender
{
     gameOver = YES;
     // your code
}

-(void)levelFinishedSuccesfully
{
     // this method will get called if user finishes the level before your timer ends/stops the level. So the timer is valid and we need to invalidate it
     if(!gameOver)
     {
          [gameOverTimer invalidate];
          gameOverTimer = nil;
     }    
      // your code
}

希望这有帮助。

You can maintain flag to save whether the timer has been fired or not.

eg.

    BOOL gameOver = NO;
    NSTimer * gameOverTimer;


-(void)startGame
{

     gameOverTimer =    [NSTimer scheduledTimerWithTimeInterval:600 target:self selector:@selector(stopLevel:) userInfo:nil repeats:NO]; 
     // your code
}

-(void)stopLevel:(id)sender
{
     gameOver = YES;
     // your code
}

-(void)levelFinishedSuccesfully
{
     // this method will get called if user finishes the level before your timer ends/stops the level. So the timer is valid and we need to invalidate it
     if(!gameOver)
     {
          [gameOverTimer invalidate];
          gameOverTimer = nil;
     }    
      // your code
}

Hope this helps.

望喜 2024-10-27 06:33:20

如果repeats为YES,定时器将重复地重新调度自己,直到失效。如果为“否”,计时器将在触发后失效。

If repeats is YES, the timer will repeatedly reschedule itself until invalidated. If NO, the timer will be invalidated after it fires.

执手闯天涯 2024-10-27 06:33:20

您缺少将计时器源添加到运行循环

addTimer:forMode:

you are missing to add the timer source to your runloop

addTimer:forMode:

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