【问题标题】:How to increase speed of timer object using Swift如何使用 Swift 提高计时器对象的速度
【发布时间】:2015-01-22 01:00:44
【问题描述】:

我正在使用 NSTimer.scheduledTimerWithTimeInterval 对象,但无法将时钟速度提高到 1/1000 以上。我曾尝试使时钟无效并重新创建。 timer2 和 timer3 在 iOS 模拟器上工作正常,但在 iPhone 5s 设备上测试时不起作用。即使在触发 timer2 或 timer3 时,速度也始终保持在 1/1000。以下是我的定义:

timer = NSTimer.scheduledTimerWithTimeInterval(0.001, target: self, selector: Selector("result"), userInfo: nil, repeats: true)

timer2 = NSTimer.scheduledTimerWithTimeInterval(0.002, target: self, selector: Selector("result"), userInfo: nil, repeats: true)

timer3 = NSTimer.scheduledTimerWithTimeInterval(0.003, target: self, selector: Selector("result"), userInfo: nil, repeats: true)

【问题讨论】:

  • 您将计时器设置为每秒触发 1000 次
  • 你所有的定时器都使用同一个选择器,你应该如何区分定时器?
  • 也许我的问题还不够清楚。我面临的问题是我的 iPhone 测试设备无法处理大于 0.001 的间隔数。当它设置为 0.002 或 0.003 时,iPhone 上的速度保持在 0.001。但是,它在 iPhone 模拟器上运行良好。我需要了解 iPhone 是否存在限制或编码问题。

标签: ios swift iphone xcode6 nstimer


【解决方案1】:

您不能更改计时器。如果您希望计时器以不同的间隔运行,则必须使现有计时器无效并销毁并用新计时器替换它。这是很常见的事情。计时器是轻量级对象。

【讨论】:

  • 马特感谢您的回复。我听取了您的建议,现在在模拟器中运行时效果很好,但是在设备上播放时计时器的速度不会改变?让我知道在此处粘贴代码是否有帮助?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-10-19
  • 1970-01-01
  • 2020-09-06
  • 2012-04-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多