1

We have a simple multiplayer table game that uses no networking connection. It has a pulsing animation which we want to play in sync with all other devices.

To do this I looked at NSDate -timeIntervalSinceReferenceDate to calculate a delay. Assuming all devices deliver the exact same value at the exact same time. But we found there is a difference of several seconds so this value is not very precise. Each device is a bit off from the true, correct time.

Is there an alternative, more precise way to get the time interval since reference date?

4

2 回答 2

2
double unixpoch = 978307200 + [[NSDate date] timeIntervalSinceReferenceDate];

...或者...

double unixepoch = [[NSDate date] timeIntervalSince1970];`

两者是相同的。不过,记下该常量非常方便,因为如果您开始将时间存储在使用 的 CoreData 中ref date,并希望在 SQLite 浏览器中查看它,那么您需要知道此 SQL 查询的常量:

SELECT ZBATTERYRECORD.*, DATETIME(zclientcreatedat + 978307200,'unixepoch') from ZBATTERYRECORD

您可能想知道 978307200 是干什么用的。iOS 将日期存储为 2001 年 1 月 1 日之后的秒数,而不是 1970 年,因此添加 978307200 秒会将日期向前移动到纪元。

于 2013-11-11T21:42:37.603 回答
2

假设所有设备在完全相同的时间提供完全相同的值。

这是一个很大(且无效)的假设,因为系统时间不太可能在多个设备上完全同步。询问当前时间取决于设备认为当前时间是什么,并且不能保证准确到亚秒级精度。

如果您想要更精确的时间,您可能需要在您的应用程序中嵌入网络时间协议代码并与时间服务器进行检查。您最好查看本地设备到设备的通信以尝试同步动画——蓝牙、wifi,甚至是让一个设备播放其他设备可以听到的声音来触发动画(例如“点击音轨”,播放重复的声音,其他设备可以同步)。

于 2013-09-01T21:29:29.050 回答