2011-04-17 37 views
2

我試圖用CMDeviceMotion跟蹤iPhone向後傾斜,然後做些事情。我已經成功創建了CMMotionManager,我從系統獲取運動數據,並且在超過特定加速度的情況下篩選結果。使用CMDeviceMotion跟蹤iPhone傾斜隨着時間的推移

我想做的事情是當設備向後或向前傾斜超過特定速度時檢測到。我怎麼做?

這裏是我到目前爲止的代碼:

更新:我想我解決了這個問題。我正在尋找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]; 
     } 
    } 
} 

回答

2

你看過CMAttitude?聽起來像你所需要的,加上我猜想的一些數學。


編輯:好的,你做了。

引用Apple documentation,在章「獲得當前設備方向」

如果你只需要知道一般 方向的裝置,而不是定向的 確切的載體,你 應該使用UIDevice 類的方法來檢索該信息。 使用UIDevice接口很簡單 ,並且不需要您自己計算方向矢量 。

很好,他們爲我們做數學。那麼我想跟蹤你的跟蹤加速跟蹤定位,正如上面的文檔解釋,應該使用UIDeviceOrientationFaceUpUIDeviceOrientationFaceDown來完成這項工作嗎?

如果沒有UIDeviceOrientation的滿足您的需求,你需要的是從兩個CMAttitude引用,它提供了一個rotationMatrixquaternion計算一些空間角......這些算術學校距離很遠,我...我然後會建議可能搜索/提出一個數學只有這些問題。

+0

這是我需要幫助的數學.. – 2011-04-17 15:41:29

+0

啊對不起,沒有看到足夠的代碼,編輯我的答案... – 2011-04-17 16:47:13

+0

我更加感興趣的加速度和旋轉速度,以檢測設備'翻轉'倒退」。當他們翻轉它時,想想一個警察和他的徽章(如果他們仍然這樣做的話)。我想我已經解決了它的大部分。我會編輯我的OP。 – 2011-04-17 16:59:15

相關問題