2

我正在尝试使用 CMDeviceMotion 来跟踪 iPhone 何时向后倾斜,然后做一些事情。我已经成功地创建了 CMMotionManager,我正在从系统中获取运动数据,并且我正在过滤某个加速度以上的结果。

不过,我想做的是检测设备何时向后或向前倾斜超过一定速度。我怎么做?

这是我到目前为止的代码:

更新:我想我解决了。我正在寻找rotationRate 属性CMRotationRate。我已经将它们配对在一起,我真的只需要 x 值,所以我会继续努力。如果有人对以下代码有一些提示,我们将不胜感激。

    - (void)startMotionManager{
        if (motionManager == nil) {
            motionManager = [[CMMotionManager alloc] init];
        }

        motionManager.deviceMotionUpdateInterval = 1/15.0;
        if (motionManager.deviceMotionAvailable) {

            NSLog(@"Device Motion Available");
            [motionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue currentQueue]
                                               withHandler: ^(CMDeviceMotion *motion, NSError *error){
                                                       //CMAttitude *attitude = motion.attitude;
                                                       //NSLog(@"rotation rate = [%f, %f, %f]", attitude.pitch, attitude.roll, attitude.yaw);
                                                   [self performSelectorOnMainThread:@selector(handleDeviceMotion:) withObject:motion waitUntilDone:YES];

                                               }];
                //[motionManager startDeviceMotionUpdates];


        } else {
            NSLog(@"No device motion on device.");
            [self setMotionManager:nil];
        }
    }

   - (void)handleDeviceMotion:(CMDeviceMotion*)motion{
    CMAttitude *attitude = motion.attitude;

    float accelerationThreshold = 0.2; // or whatever is appropriate - play around with different values
    CMAcceleration userAcceleration = motion.userAcceleration;

    float rotationRateThreshold = 7.0;
    CMRotationRate rotationRate = motion.rotationRate;

    if ((rotationRate.x) > rotationRateThreshold) {
        if (fabs(userAcceleration.x) > accelerationThreshold || fabs(userAcceleration.y) > accelerationThreshold || fabs(userAcceleration.z) > accelerationThreshold) {

            NSLog(@"rotation rate = [Pitch: %f, Roll: %f, Yaw: %f]", attitude.pitch, attitude.roll, attitude.yaw);
            NSLog(@"motion.rotationRate = %f", rotationRate.x);

            [self showMenuAnimated:YES];
        }
    }

    else if ((-rotationRate.x) > rotationRateThreshold) {
        if (fabs(userAcceleration.x) > accelerationThreshold || fabs(userAcceleration.y) > accelerationThreshold || fabs(userAcceleration.z) > accelerationThreshold) {

            NSLog(@"rotation rate = [Pitch: %f, Roll: %f, Yaw: %f]", attitude.pitch, attitude.roll, attitude.yaw);
            NSLog(@"motion.rotationRate = %f", rotationRate.x);

            [self dismissMenuAnimated:YES];
        }
    }
}
4

1 回答 1

2

你看过CMAtitude吗?听起来像你需要的,加上我猜的一些数学。


编辑:好的,你做到了。

在“获取当前设备方向”一章中引用Apple 文档

如果您只需要知道设备的大致方向,而不是确切的方向向量,则应该使用 UIDevice 类的方法来检索该信息。使用 UIDevice 接口很简单,不需要你自己计算方向向量。

太好了,他们为我们做数学。然后我猜想像你这样的跟踪加速+跟踪方向,正如上面的文档解释的那样,应该使用UIDeviceOrientationFaceUpand来完成这项工作UIDeviceOrientationFaceDown

如果不UIDeviceOrientation符合您的需求,您将需要从两个CMAttitude参考中计算一些空间角度,这提供了 arotationMatrix和 a quaternion......这些学校数学对我来说很遥远......然后我建议也许搜索/询问一个只有数学的问题。

于 2011-04-17T15:31:46.963 回答