使用CMDeviceMotion跟踪iPhone随时间的倾斜。

| 我正在尝试使用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];
        }
    }
}
    
已邀请:
您是否看过CMAttitude?听起来像您所需要的,再加上一些我猜想的数学。 编辑:好的,你做到了。 在“获取当前设备方向”一章中引用Apple文档:   如果您只需要了解一般   设备的方向,而不是   方向的精确向量,你   应该使用UIDevice的方法   类以检索该信息。   使用UIDevice接口很简单   并不需要你   计算方向向量   你自己 太好了,他们为我们做数学。然后我想像上面的文档所解释的那样,像您一样跟踪加速度+跟踪方向,应该使用
UIDeviceOrientationFaceUp
UIDeviceOrientationFaceDown
完成这项工作吗? 如果the3ѭ都不符合您的需求,则需要从两个
CMAttitude
引用中计算一些空间角度,从而提供provides5ѭ和a6ѭ...这些学校数学对我来说很遥远...然后我建议也许用这些来搜索/询问仅数学问题。     

要回复问题请先登录注册