0

我想做的是阅读acceleration.y并执行以下操作:

if (acceleration.y > 0.8) {
   // Do something
}

由于不推荐使用 didAccelerate,我想知道如何获得 y 值:

motionManager = [[[CMMotionManager alloc] init] autorelease];
motionManager.accelerometerUpdateInterval = kUpdateInterval;

if (motionManager.accelerometerAvailable) {
    [motionManager startAccelerometerUpdates];
}
else {
    //this device doesn't have accelerometer notice somewhere??
}

- (void)startAccelerometerUpdates {
 // READ Y-VALUE?????
}

我想使用原始加速度计数据,因此该应用程序也适用于 3GS。是否可以读取 Y 值?

4

2 回答 2

2

编辑:不推荐使用以下答案,请检查这些帖子以了解正确的方法。


老答案:

为此使用 UIAccelerometer 单例实例,例如在您的 AppDelegate 中

//in your launching method
    UIAccelerometer * accel = [UIAccelerometer sharedAccelerometer];
    accel.delegate = self;

//delegate method:
-(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration{
    // use the y-property of the acceleration
}
于 2011-12-28T14:22:48.157 回答
0

查看名为“Handing Motion Updates at Specified Intervals”的CMMotionManager 类参考和搜索部分。在“加速度计”项目符号中,它说

设置 accelerometerUpdateInterval 属性以指定更新间隔。调用 startAccelerometerUpdatesToQueue:withHandler: 方法,传入一个 CMAccelerometerHandler 类型的块。加速度计数据作为 CMAccelerometerData 对象传递到块中。

CMAccelerometerData具有对 CMAcceleration 的引用,CMAcceleration 是一个保存每个轴加速度的结构。

于 2011-12-28T16:36:36.907 回答