OGeek|极客世界-中国程序员成长平台

标题: ios - 如何以秒为单位确定CMDeviceMotion的年龄 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 10:03
标题: ios - 如何以秒为单位确定CMDeviceMotion的年龄

我希望能够在我的游戏过程中打开和关闭设备动态更新。但是在我关闭它们然后再次返回后,CMMotionManager 的 .deviceMotion 属性会在短时间内返回最后知道的 deviceMotion(大约 0.5 秒,直到它可以计算出新值,我猜)。

所以,我希望能够测试我获得的 CMDeviceMotion 对象的年龄,如果它们超过几秒钟,则忽略它们。

计算 CACurrentMediaTime() - deviceMotion.timestamp 似乎给了我准确的结果。但我担心这可能会在未来的设备或 iOS 版本中中断。设备运动时间戳是否保证在未来与 CACurrentMediaTime() 保持可比性?有没有更好的方法来计算 CMDeviceMotion 时间戳的年龄?

来自 Apple 的文档:

CACurrentMediaTime()

A CFTimeInterval derived by calling mach_absolute_time() and converting the result to seconds.

deviceMotion.timestamp

The time stamp is the amount of time in seconds since the phone booted.



Best Answer-推荐答案


我遇到了同样的问题,需要保留事件历史记录。我通过将 updateInterval 设置为非常低的值来解决这个问题,例如0.5。因此,Core Motion 无需重新初始化传感器,从而降低能耗。

关于ios - 如何以秒为单位确定CMDeviceMotion的年龄,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15837517/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4