2012-12-21 61 views
0

現在我正在使用以下代碼從設備的陀螺儀中獲取歐拉值。這是應該如何使用?或者沒有使用NSTimer有更好的方法嗎?我是否需要使用NSTimer從陀螺儀iOS獲取數據?

- (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; 
} 
+0

對我來說看起來不錯:) –

+0

我的目標是持續監測偏航值,以確定設備是否沿其中心旋轉了360度。什麼是最有效的方式來做到這一點? – thisiscrazy4

回答

1

您可以使用此...

[motionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue currentQueue] withHandler:^(CMDeviceMotion *motion, NSError *error) 
{ 
    CMAttitude *attitude; 
    attitude = motion.attitude; 
    int yaw = attitude.yaw; 
}]; 
1

直接引用the documentation:在指定的時間間隔

處理動態更新要接收在特定時間間隔的運動數據 ,該應用程序調用一個「開始」的方法,需要一個 操作隊列(NSOperationQueue的實例)和一個處理這些更新的特定類型的塊處理程序 。運動數據是 傳遞到塊處理程序。更新的頻率由「間隔」屬性的值確定爲 。

[...]

設備運動。設置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一個更好的解決方案,因爲它會給運動經理對你關心多久更新一個明確的線索。