以毫秒Xcode显示游戏时间

时间:2014-05-23 00:52:26

标签: ios xcode nstimer

所以我在玩一些应用程序,我试图让游戏计时器在几毫秒而不是正常的1,2,3。我希望它的格式类似于1.000,2.000。甚至是1.00,2.00。

这是我的代码:

(void)startTimer {
    if (count == 30) {
      // 3
    timer = [NSTimer scheduledTimerWithTimeInterval:1.0
                                             target: self
                                           selector:@selector(ElapsedTime)
                                           userInfo:nil
                                            repeats:YES];

1 个答案:

答案 0 :(得分:2)

使用

记录开始时间
  

NSTimeInterval start = [NSDate timeIntervalSinceReferenceDate];

如果您想查看已经过了多长时间,请使用:

  

NSTimeInterval elapsed = [NSDate timeIntervalSinceReferenceDate] -   启动;

elapsed将在几秒钟内完成。如果你想要毫秒,减去整数来得到小数部分。将小数部分乘以1000得到毫秒。

如果您希望每秒更换一次,请将计时器的时间间隔设为小于1.0秒。请注意,定时器的准确度不超过约1/50秒。