如何管理我的NSTimer在tabbar iPhone应用程序?

时间:2022-06-24 07:15:24

I have a tabbar-based app with three tabs, one of them is a running clock. To animate the clock UI, I use an NSTimer that fires once a second. If the user will be switching in and out of the various tabs, how should I manage this timer?

我有一个基于tabbar的应用,有三个标签,其中一个是运行时钟。为了使时钟UI具有动画效果,我使用一个每秒触发一次的NSTimer。如果用户将切换到不同的选项卡,我应该如何管理这个计时器?

  1. Can I just use scheduledTimerWithTimeInterval to create and schedule the timer when the app is first run and let it die when the app closes? Or should I maintain an instance variable for the timer in the rootview controller for the clock tab and use addTimer:forMode: and invalidate?
  2. 我能不能用scheduledTimerWithTimeInterval在app首次运行时创建并调度计时器,在app关闭时让它死?或者,我应该在rootview控制器中为时钟选项卡维护一个计时器实例变量,并使用addTimer:forMode:和invalidate吗?
  3. If the ladder, how often should I add and invalidate the timer? In my viewWillAppear and viewWillDisappear methods for the rootview controller of the clock tab? Somewhere else?
  4. 如果是*,我应该多长时间添加和失效计时器?在我的viewWillAppear和viewWillDisappear方法中,用于时钟选项卡的rootview控制器?别的地方吗?

Thanks so much in advance for your wisdom!

非常感谢您的智慧!

2 个解决方案

#1


1  

I would do the following:

我要做的是:

if your Clock-View appears -> set the Clock to the current time, then start a timer that updates the clock every second:

如果您的时钟视图出现——>将时钟设置为当前时间,然后启动一个计时器,每秒钟更新一次时钟:

clockRefreshTimer = [NSTimer scheduledTimerWithTimeInterval: 1 target: self selector:@selector(updateClockView) userInfo:nil repeats:YES]

if the clock view is about to disappear invalidate the timer

如果时钟视图即将消失,则使计时器无效

[clockRefreshTimer invalidate];

This way you don't do unnecessary updates.

这样你就不会做不必要的更新。

hope I could help,
sam

希望我能帮忙,萨姆

#2


2  

What @samsam said, plus this: a timer set to 1 second is NOT guaranteed to fire precisely on the second. It'll be scheduled for the next available processing time once a second has passed. In many cases it'll be pretty darn close to the exact time it's scheduled to go, but rarely will it be exactly then, and it might be off by quite a bit if the app is busy with other things.

@samsam说的,再加上这个:设置为1秒的计时器不能保证在第2秒就能精确射击。一旦一秒钟过去,它将被安排到下一个可用的处理时间。在很多情况下,它会非常接近预定的时间,但很少会是准确的时间,如果应用程序忙于其他事情,它可能会关闭很多。

If it's really critical that your clock actually tick once a second (and it wouldn't be much of a clock if it didn't), you want your timer to fire more often than that, and update the UI when it notices that the time value it's watching has changed.

如果你的时钟真的每秒钟滴答一次是非常重要的(如果不是的话,它也不会是一个时钟),你希望你的计时器能更频繁地启动,并且当它注意到它正在观察的时间值发生变化时更新UI。

#1


1  

I would do the following:

我要做的是:

if your Clock-View appears -> set the Clock to the current time, then start a timer that updates the clock every second:

如果您的时钟视图出现——>将时钟设置为当前时间,然后启动一个计时器,每秒钟更新一次时钟:

clockRefreshTimer = [NSTimer scheduledTimerWithTimeInterval: 1 target: self selector:@selector(updateClockView) userInfo:nil repeats:YES]

if the clock view is about to disappear invalidate the timer

如果时钟视图即将消失,则使计时器无效

[clockRefreshTimer invalidate];

This way you don't do unnecessary updates.

这样你就不会做不必要的更新。

hope I could help,
sam

希望我能帮忙,萨姆

#2


2  

What @samsam said, plus this: a timer set to 1 second is NOT guaranteed to fire precisely on the second. It'll be scheduled for the next available processing time once a second has passed. In many cases it'll be pretty darn close to the exact time it's scheduled to go, but rarely will it be exactly then, and it might be off by quite a bit if the app is busy with other things.

@samsam说的,再加上这个:设置为1秒的计时器不能保证在第2秒就能精确射击。一旦一秒钟过去,它将被安排到下一个可用的处理时间。在很多情况下,它会非常接近预定的时间,但很少会是准确的时间,如果应用程序忙于其他事情,它可能会关闭很多。

If it's really critical that your clock actually tick once a second (and it wouldn't be much of a clock if it didn't), you want your timer to fire more often than that, and update the UI when it notices that the time value it's watching has changed.

如果你的时钟真的每秒钟滴答一次是非常重要的(如果不是的话,它也不会是一个时钟),你希望你的计时器能更频繁地启动,并且当它注意到它正在观察的时间值发生变化时更新UI。