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

CMAttitudeを見たことがありますか?あなたが必要としているもののように聞こえます、そして私が推測するいくつかの数学。


編集:わかりました。

「現在のデバイスの向きを取得する」の章で、Appleのドキュメントを引用します。

デバイスの一般的な方向のみを知り、方向の正確なベクトルを知る必要がない場合は、UIDeviceクラスのメソッドを使用してその情報を取得する必要があります。UIDeviceインターフェイスの使用は簡単で、方向ベクトルを自分で計算する必要はありません。

素晴らしい、彼らは私たちのために数学をやってくれます。次に、上記のドキュメントで説明されているように、あなたのような加速の追跡+方向の追跡は、とを使用して仕事をする必要があるUIDeviceOrientationFaceUpUIDeviceOrientationFaceDown思いますか?

ニーズに合わない場合は、2つの参照UIDeviceOrientationからいくつかの空間角度を計算する必要があります。これにより、aと...が提供されます。これらの学校の数学は私には遠いです...次に、検索/質問することをお勧めします。これらに関する数学のみの質問。CMAttituderotationMatrixquaternion

于 2011-04-17T15:31:46.963 に答える