iPhone加速计:didAccelerate:当我运行循环时似乎没有被调用

发布于 2024-08-29 18:36:46 字数 894 浏览 5 评论 0原文

一个与加速度计相关的问题。 (抱歉,格式可能看起来不正确,这是我第一次使用这个网站)。我使用标准代码让加速度计按预期工作,

UIAccelerometer *accel = [UIAccelerometer sharedAccelerometer];
accel.delegate = self;
accel.updateInterval = 0.1;   //I also tried other update values

每次调用类中的加速度计:didAccelerate:方法时,我都会使用 NSLog 进行记录。该函数按预期被调用,并且一切正常,直到这里。

但是,当我运行循环时,上述方法似乎没有被调用。像这样的事情

float firstAccelValue = globalAccel; //this is the x-accel value (stored in a global by the above method)
float nextAccelValue = firstAccelValue;

while (nextAccelValue == firstAccelValue){

    //do something
    nextAccelValue = globalAccel; // note globalAccel is updated by the accelerometer method

}

上面的循环永远不会退出,这是预期的,因为加速度计:didAccelerate:方法没有被调用,因此 globalAccel 永远不会改变值。

如果我使用固定条件来中断 while 循环,我可以看到循环结束后,方法调用再次正常工作。

我在这里遗漏了一些明显的东西吗?或者在进行某些处理时加速度计方法不会触发?

任何帮助将不胜感激!谢谢!

An accelerometer related question. (Sorry the formatting may not look right, its the first time I am using this site). I got the accelerometer working as expected using the standard code

UIAccelerometer *accel = [UIAccelerometer sharedAccelerometer];
accel.delegate = self;
accel.updateInterval = 0.1;   //I also tried other update values

I use NSLog to log every time the accelerometer:didAccelerate: method in my class is called. The function gets called as expected and everything works fine till here.

However, when I run a loop, the above method doesn't seem to get called. Something like this

float firstAccelValue = globalAccel; //this is the x-accel value (stored in a global by the above method)
float nextAccelValue = firstAccelValue;

while (nextAccelValue == firstAccelValue){

    //do something
    nextAccelValue = globalAccel; // note globalAccel is updated by the accelerometer method

}

The above loop never exits, expectedly since the accelerometer:didAccelerate: method is not getting called, and hence globalAccel never changes value.

If I use a fixed condition to break the while loop, I can see that after the loop ends, the method calls work fine again.

Am I missing something obvious here? Or does the accelerometer method not fire when certain processing is being done?

Any help would be greatly appreciated! Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

表情可笑 2024-09-05 18:36:46

(不要将 float== 进行比较。)

CPU 被循环占用,没有时间为您提供更新的加速器值。

既然每次加速度变化时都会调用 -accelerometer:didAccelerate: ,为什么不直接使用 if 呢?

// in -accelerometer:didAccelerate:
if (fabs(nextAccelValue - firstAccelValue) < 0.0001) {
   // do something
   nextAccelValue = globalAccel;
}

(Don't compare float with ==.)

The CPU is occupied by the loop and has no time to give you the updated accelerator value.

Since -accelerometer:didAccelerate: is called everytime the acceleration changes, why not just use if?

// in -accelerometer:didAccelerate:
if (fabs(nextAccelValue - firstAccelValue) < 0.0001) {
   // do something
   nextAccelValue = globalAccel;
}
甜扑 2024-09-05 18:36:46

非常感谢! (关于浮动比较,是的我明白,这只是一个例子)。

实际上,我想做的是这样的:按下按钮后,我想获取加速度计值(firstValue),然后等待它发生变化(即相对于firstValue的特定值),然后继续执行一些任务。因此我使用的是 while 循环。 (我展示的循环只是等待它发生变化,但一旦它起作用,我就可以输入所需的确切变化条件)。

从您的回答中,我了解到我可以在 -accelerometer:didAccelerate: 函数本身中执行该任务(因为它可以访问它需要的所有数据,我可以让它访问一个指示按钮是否被按下的变量)。非常感谢。我想我可以用这种方式让它工作。

但只是好奇 - 有没有办法做到这一点?如果过多的处理占用了 CPU,我可以强制它以某种方式更新加速度计吗?占用 CPU 本身的循环优先级较低,因为它只是我等待值更改的方式。

再次感谢!

Thanks much for that! (About the float comparison, yes I understand, it was just an example).

Actually, what I want to do is this: After a button is pressed, I want to obtain the accelerometer value (firstValue) and then wait until it changes (to say a particular value relative to firstValue) and then proceed to do some tasks. As such I was using the while loop. (The loop I show just waits until it changes, but I can put the exact change condition required once it works).

From your answer, I understand that I can perform the task in the -accelerometer:didAccelerate: function itself (since it has access to all the data it needs, I can make it access a variable indicating whether the button was pressed). Thanks very much for that. I guess I can make it work in this way.

But just curious - is there a way to do it otherwise? If too much processing is hogging the CPU, can I force it to update accelerometer somehow? The loop hogging the CPU itself is low priority since it was just my way of waiting until the value changes.

Thanks again!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文