Accuracy of NSTimer

后端 未结 4 1350
时光取名叫无心
时光取名叫无心 2020-12-03 12:59

I am trying to use NSTimer to create a Stop-watch style timer that increments every 0.1 seconds, but it seems to be running too fast sometimes ..

This is how I\'ve

4条回答
  •  长情又很酷
    2020-12-03 13:23

    maxTime=maxTime+0.1;
    

    This is the wrong way to go. You don't want to use a timer to accumulate the elapsed time because you'll be accumulating error along with it. Use the timer to periodically trigger a method that calculates the elapsed time using NSDate, and then updates the display. So, change your code to do something instead:

    maxTime = [[NSDate date] timeIntervalSince:startDate];
    

提交回复
热议问题