编辑:添加了一个全局变量,现在可以正常工作了。但我仍然有疑问.. 请继续阅读:)
我想在需要时在 Y 轴上执行加速度,并在我的代码的不同部分使用它。在这个例子中,我在一个 while 循环中使用它来进行测试。
我的代码可以正常工作,但我是否正确使用了 UpdateToQueue... 方法,还是这种实现我想要的东西的“非正统”方式?
我将更新间隔设置为 30 毫秒,您认为这是一个“安全”的更新间隔吗?我被告知在选择一个时我应该小心,因为当前或以后的硬件/iOS 更新可能无法跟上这样的间隔,这是真的吗?
double myAcceleration; // a global..
-(void) play // my "main" method..
{
CMMotionManager *motionManager = [[CMMotionManager alloc] init];
motionManager.deviceMotionUpdateInterval = 0.03; // update every 30ms
[motionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue mainQueue]
withHandler:^(CMDeviceMotion *motion, NSError *error)
{
myAcceleration = motion.userAcceleration.y;
}
];
while(!self.stopButtonPressed)
{
NSLog(@"Y-Axis acceleration is %f", myAcceleration);
}
}
最佳答案
最后。我找到了我自己问题的答案:)
苹果文档:
Choosing a Motion Event Update Interval
When you request motion data with Core Motion, you specify an update interval. You should choose the largest interval that meets your app’s needs. The larger the interval, the fewer events are delivered to your app, which improves battery life.
Table 4-1 lists some common update frequencies and explains what you can do with data generated at that frequency. Few apps need acceleration events delivered 100 times a second.
Event frequency (Hz) Usage
10–20 Suitable for determining a device’s current orientation vector.
30–60 Suitable for games and other apps that use the accelerometer for real-time user input.
70–100 Suitable for apps that need to detect high-frequency motion. For example, you might use this interval to detect the user hitting the device or shaking it very quickly.
You can set the reporting interval to be as small as 10 milliseconds (ms), which corresponds to a 100 Hz update rate, but most app operate sufficiently with a larger interval.
简而言之,正如 Bogdan 所说:10Hz-100Hz
关于iphone - startDeviceMotionUpdatesToQueue 的安全更新间隔 :withHandler:?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11797857/