Решение

We use cookies. Read the Privacy and Cookie Policy

Решение

Пользуйтесь методом экземпляра startAccelerometerUpdatesToQueue: withHandler:, относящимся к классу CMMotionManager. Вот заголовочный файл контроллера вида, в котором класс CMMotionManager применяется для получения обновлений от акселерометра:

#import «ViewController.h»

#import <CoreMotion/CoreMotion.h>

@interface ViewController ()

@property (nonatomic, strong) CMMotionManager *motionManager;

@end

@implementation ViewController

Мы реализуем контроллер вида и воспользуемся методом startAccelerometerUpdatesToQueue: withHandler: класса CMMotionManager:

— (void)viewDidLoad{

[super viewDidLoad];

self.motionManager = [[CMMotionManager alloc] init];

if ([self.motionManager isAccelerometerAvailable]){

NSOperationQueue *queue = [[NSOperationQueue alloc] init];

[self.motionManager

startAccelerometerUpdatesToQueue: queue

withHandler: ^(CMAccelerometerData *accelerometerData, NSError *error) {

NSLog(@"X = %.04f, Y = %.04f, Z = %.04f",

accelerometerData.acceleration.x,

accelerometerData.acceleration.y,

accelerometerData.acceleration.z);

}];

} else {

NSLog(@"Accelerometer is not available.");

}

}

Данный текст является ознакомительным фрагментом.