gpt4 book ai didi

ios - "Precise"ios 中周期的采样时序

转载 作者:行者123 更新时间:2023-11-29 04:12:55 24 4
gpt4 key购买 nike

我定期向遥控直升机发送数据,目前我使用 NSTimer,时间间隔为 30 毫秒来执行此操作。现在,由于 NSTimer 的不精确性和 ios 的非实时性,我与采样时间出现了很大的差异。 (前 30 毫秒内有 5 个数据包,然后 4 个周期内没有任何数据,依此类推)。有没有更精确的方法来处理ios中的数据采样和函数计时?

最佳答案

您可以像在游戏中通常做的那样来微调 fps(并且将逻辑和绘画分开以使其在任何 fps 下都能正常工作)。像这样的东西,并在后台线程中调用它:

- (void)updateLoop
{
NSTimeInterval last = CFAbsoluteTimeGetCurrent();
NSTimeInterval elapsed = 0;
while (self.running) {
NSTimeInterval now = CFAbsoluteTimeGetCurrent();
NSTimeInterval dt = now - last;

elapsed += dt;

if (elapsed >= YOUR_INTERVAL) {
//Do stuff
elapsed = 0;
}

last = now;

[NSThread sleepForTimeInterval:0.001]; //There is not NSThread yield
}
}

关于ios - "Precise"ios 中周期的采样时序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14143195/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com