我正在尝试在手机上获取加速度计值并将其返回到函数中。以下代码永远不会到达“已达到”的NSLog。我在viewDidLoad中调用它,它进入if语句。任何帮助是极大的赞赏。
- (NSArray *)getAccelerometerValues{
//Get the current x y and z
__block float x, y, z;
CMMotionManager *motionManager = [[CMMotionManager alloc] init];
motionManager.accelerometerUpdateInterval = .5;
if(motionManager.accelerometerAvailable){
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[motionManager startAccelerometerUpdatesToQueue:queue
withHandler:^(CMAccelerometerData *accelerometerData, NSError * error) {
NSLog(@"Reached");
x = (accelerometerData.acceleration.x * 180) / M_PI;
y = (accelerometerData.acceleration.y * 180) / M_PI;
z = (accelerometerData.acceleration.z * 180) / M_PI;
NSLog(@"X: %f Y: %f Z: %f",x,y,z);
}];
}
NSArray *xyz = [[NSArray alloc] initWithObjects:
[NSNumber numberWithFloat:x],
[NSNumber numberWithFloat:y],
[NSNumber numberWithFloat:z],
nil];
return xyz;