xcode 如何从 iOS 上的 Core Motion 获取数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9161959/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to get data from Core Motion on iOS
提问by Chris Lin
i have trouble getting the accelerometer data from core motion manager...i followed the documentation and it still doesn't work :(
我无法从核心运动管理器获取加速度计数据……我按照文档进行操作,但它仍然无法正常工作:(
self.manager = [[CMMotionManager alloc] init];
self.manager.accelerometerUpdateInterval = 0.01;
[self.manager startAccelerometerUpdates];
CMAccelerometerData *newestAccel = self.manager.accelerometerData;
int x, y, z;
x = newestAccel.acceleration.x;
y = newestAccel.acceleration.y;
z = newestAccel.acceleration.z;
any help would be very much appreciated!!
任何帮助将不胜感激!
采纳答案by Carl Goldsmith
I managed to make it work using blocks, here's the code that I managed to get working:
我设法使用块使其工作,这是我设法开始工作的代码:
NSOperationQueue *theQueue = [[NSOperationQueue alloc] init];
_returnedData = [[CMAccelerometerData alloc] init];
_motionManager = [[CMMotionManager alloc] init];
[_motionManager startAccelerometerUpdatesToQueue:theQueue withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) {
_returnedData = _motionManager.accelerometerData;
int x = _motionManager.accelerometerData.acceleration.x;
int y = _returnedData.acceleration.y;
NSLog(@"X: %i, Y: %i", x, y);
}];
You can either access the accelerometerData.accelleration directly from the CAMotionManager or by creating an instance of CMAccelerometerData and assigning the variables to that. Hope this helps.
您可以直接从 CAMotionManager 访问 accelerometerData.accelleration 或通过创建 CMAccelerometerData 的实例并将变量分配给它。希望这可以帮助。

