4

我正在使用 xcode 4.2(并且将很快升级)和这些代码行

[[UIAccelerometer sharedAccelerometer] setUpdateInterval:(1.0 / kAccelerometerFrequency)];
[[UIAccelerometer sharedAccelerometer] setDelegate:self];

代码有效,但我有一个黄色警告说代码是deprecated,我查看了新的参考,CoreMotion现在做加速度计。我想知道写上述内容的新方法是什么..?

4

1 回答 1

7

Start accelerometer.

- (void)startAccelerometerData {
    motionManager = [[CMMotionManager alloc] init];
    motionManager.accelerometerUpdateInterval = 1/60.0;

    [motionManager startAccelerometerUpdatesToQueue:[NSOperationQueue currentQueue]
                                        withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) {
        NSLog(@"x:= %f y:= %f z:= %f", accelerometerData.acceleration.x, accelerometerData.acceleration.y, accelerometerData.acceleration.z);
    } ];
}

stop accelerometer

- (void)viewDidUnload  {
    [motionManager stopAccelerometerUpdates];
    motionManager = nil;
    [super viewDidUnload];
}
于 2012-05-30T09:32:18.420 回答