How to get data from Core Motion on iOS

I am having problems getting accelerometer data from the main motion controller ... I followed the documentation and still does not 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 greatly appreciated!

+4
source share
1 answer

I managed to get it to work using blocks, here is the code I managed to get:

 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 Data.accelleration accelerator directly from the CAMotionManager, or create an instance of CMAccelerometerData and assign variables to it. Hope this helps.

+4
source

Source: https://habr.com/ru/post/1395032/


All Articles