Accuracy of NSTimer

后端 未结 4 1352
时光取名叫无心
时光取名叫无心 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:15

    NSTimer is not guaranteed to be accurate, although in practice it usually is (if you're not doing anything else on your main thread...). However, it's perfectly reasonable for updating a display... just don't use the callback to calculate your timer. Save the current time when you start your timer, and get the difference between now and when you started every time the timer fires. Then it doesn't really matter how accurately NSTimer is firing, it only impacts how many times a second your on screen display updates.

提交回复
热议问题