我正在研究需要從iPhone檢測旋轉手勢的應用程序。我已經編寫了輪詢CMMotionManager輪換數據的代碼,但由於某些原因,即使手機靜止在桌面上,這些值也會不斷變化。我不確定我在這裏做錯了什麼。我已經諮詢了蘋果的文檔,看起來我正在做他們的建議,而且這個東西確實沒有崩潰地運行,但是出來的數字沒有意義。下面是我在做什麼:Core Motion陀螺旋轉出現問題
-(void)startDetectingMotion {
if (!motionQueue){
motionQueue = [[NSOperationQueue mainQueue] retain];
}
if (motionManager.isDeviceMotionAvailable) {
CMDeviceMotionHandler motionHandler =^(CMDeviceMotion *motion, NSError *error) {
[self processMotion:motion withError:error];
};
[motionManager startDeviceMotionUpdatesToQueue:motionQueue withHandler:motionHandler];
}
else {
NSLog(@"motion not available");
}
}
.....
-(void)processMotion:(CMDeviceMotion *)motion withError:(NSError *)error {
CMRotationRate rotation = motion.rotationRate;
if(rotation.y > 2 || rotation.y < -2) {
NSLog(@"CM Motion X rotation:%f, Y rotation:%f, Z Rotation:%f", rotation.x, rotation.y, rotation.y);
....
[self stopDetectingMotion];
}
}
俘獲了Y的點> 2或2 <是看的iPhone的快速旋轉在水平面上,這是我正在尋找的。
輸出看起來像這樣:
2010-08-15 16:15:43.475 PokerFoldTest [539:307] CM運動X旋轉:11.415660,Y旋轉:7.865920,Z旋轉:7.865920
2010-08-15 16:04:33.843 PokerFoldTest [539:307] CM Motion X旋轉:8.925084,Y旋轉:8.414384,Z旋轉:8.414384
2010-08-15 16:11:14.314 PokerFoldTest [539: 307] CM Motion X旋轉:10.245130,Y旋轉:-8.243847,Z旋轉:-8.243847
2 010-08-15 16:11:16.136 PokerFoldTest [539:307] CM Motion X旋轉:10.212860,Y旋轉:-4.303616,Z旋轉:-4.303616
2010-08-15 16:11:18.242 PokerFoldTest [ 539:307] CM Motion X旋轉:9.988654,Y旋轉:-7.074587,Z旋轉:-7.074587
2010-08-15 16:11:19.678 PokerFoldTest [539:307] CM Motion X rotation:16.092894,Y旋轉:-10.562743,Z旋轉:-10.562743
2010-08-15 16:15:41.662 PokerFoldTest [539:307] CM運動X旋轉:12.854285,Y旋轉:7.247667,Z旋轉:7.247667
因爲這些數字是suppo sed爲弧度/秒旋轉,他們暗示當手機靜止在桌上時,它正在瘋狂地旋轉。我勒個去?任何機會我的手機有一個不好的陀螺儀?
奇怪的是,事情現在正常。我甚至不知道我爲了使它工作而改變了什麼,但現在它可以工作。除非我給它一個很好的翻轉,否則所有的旋轉值現在都在0左右。 – fogelbaby 2010-08-18 13:43:48