0

現在、次のコードを使用して、デバイスのジャイロスコープからオイラー値を取得しています。これはどのように使用されることになっていますか?または、NSTimerを使用せずにもっと良い方法はありますか?

- (void)viewDidLoad {
[super viewDidLoad];
CMMotionManager *motionManger = [[CMMotionManager alloc] init];
[motionManger startDeviceMotionUpdates];

NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:(1/6) target:self selector:@selector(read) userInfo:nil repeats:YES];
}

- (void)read {
CMAttitude *attitude;
CMDeviceMotion *motion = motionManger.deviceMotion;
attitude = motion.attitude;
int yaw = attitude.yaw; 
}
4

2 に答える 2

1

あなたはこれを使うことができます...

    [motionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue currentQueue] withHandler:^(CMDeviceMotion *motion, NSError *error)
 {
     CMAttitude *attitude;
     attitude = motion.attitude;
     int yaw = attitude.yaw; 
 }];
于 2012-12-21T06:42:06.300 に答える
1

ドキュメントを直接引用するには:

指定した間隔でのモーション更新の処理

特定の間隔でモーション データを受信するために、アプリは操作キュー (NSOperationQueue のインスタンス) と、これらの更新を処理するための特定のタイプのブロック ハンドラーを受け取る「開始」メソッドを呼び出します。モーション データはブロック ハンドラに渡されます。更新の頻度は、「interval」プロパティの値によって決まります。

[...]

デバイスの動き。deviceMotionUpdateInterval プロパティを設定して更新間隔を指定します。または startDeviceMotionUpdatesUsingReferenceFrame:toQueue:withHandler: または startDeviceMotionUpdatesToQueue:withHandler: メソッドを呼び出して、タイプ CMDeviceMotionHandler のブロックを渡します。前者の方法 (iOS 5.0 の新機能) では、姿勢推定に使用する参照フレームを指定できます。回転率データは、CMDeviceMotion オブジェクトとしてブロックに渡されます。

だから例えば

motionManger.deviceMotionUpdateInterval = 1.0/6.0; // not 1/6; 1/6 = 0
[motionManager 
    startDeviceMotionUpdatesToQueue:[NSOperationQueue mainQueue]
    withHandler:
        ^(CMDeviceMotion *motion, NSError *error)
         {
             CMAttitude *attitude;
             attitude = motion.attitude;
             int yaw = attitude.yaw; 
         }];

メイン キューを怠惰に使用しましたが、NSTimer よりも優れたソリューションである可能性があります。これにより、モーション マネージャーに更新頻度に関する明確な手がかりが得られるからです。

于 2012-12-21T06:42:18.187 に答える