For my game loop and certain animations I want to know the time interval that has passed since the last call of my render: method.
对于我的游戏循环和某些动画,我想知道自上次调用render:方法以来经过的时间间隔。
For this I use the CADisplayLink and grab the current timestamp and subtract the timestamp of the last call. This should give me the correct time interval between two frames/calls.
为此,我使用CADisplayLink并获取当前时间戳并减去最后一次调用的时间戳。这应该给我两个帧/调用之间的正确时间间隔。
When running the app in the simulator, I get all sorts of different values for the interval, which seem fine. If I run the app on the iPad however, I only get two different values for the interval: 0.000 and 0.125. Most of the time these two values alternate.
在模拟器中运行应用程序时,我会获得间隔的各种不同值,这看起来很好。但是,如果我在iPad上运行应用程序,我只会得到两个不同的值:0.000和0.125。大多数情况下,这两个值交替出现。
Here is the code I use to determine the interval.
这是我用来确定间隔的代码。
// Time calculations (for animations and stuff)
_lastCallTime = _currentCallTime;
_currentCallTime = [displayLink timestamp];
float timeInterval = _currentCallTime - _lastCallTime;
NSLog(@"Time since last call: %f", timeInterval);
This code gets executed every time the render method gets called.
每次调用render方法时都会执行此代码。
Is this some restriction of the iPad, that it won't give me accurate results for my interval?
这是iPad的一些限制,它不能给我准确的间隔时间结果吗?
1 个解决方案
#1
1
The timestamp is a double. Change any variable that saves or uses it as a float to NSTimeInterval. When you print it use %f (which is really %lf as C promotes floats to doubles when passed as parameters).
时间戳是双倍的。将任何保存或使用它的变量更改为NSTimeInterval。当你打印它时使用%f(这实际上是%lf,因为当作为参数传递时,C促进浮动加倍)。
#1
1
The timestamp is a double. Change any variable that saves or uses it as a float to NSTimeInterval. When you print it use %f (which is really %lf as C promotes floats to doubles when passed as parameters).
时间戳是双倍的。将任何保存或使用它的变量更改为NSTimeInterval。当你打印它时使用%f(这实际上是%lf,因为当作为参数传递时,C促进浮动加倍)。