【发布时间】:2010-08-15 20:36:52
【问题描述】:
我正在开发一个需要从 iPhone 检测旋转手势的应用。我编写了轮询 CMMotionManager 以获取旋转数据的代码,但由于某种原因,即使手机静止在桌子上,这些值也会不断变化。我不确定我在这里做错了什么。我已经咨询了 Apple 文档,似乎我正在按照他们的建议进行操作,并且事情确实运行而没有崩溃,但是出来的数字毫无意义。这是我正在做的事情:
-(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 或
输出如下:
2010-08-15 16:15:43.475 PokerFoldTest[539:307] CM Motion 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
2010-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 旋转:16.092894,Y 旋转:-10.562743,Z 旋转:-10.562743
2010-08-15 16:15:41.662 PokerFoldTest[539:307] CM Motion X 旋转:12.854285,Y 旋转:7.247667,Z 旋转:7.247667
由于这些数字应该是弧度/秒旋转,它们表明当手机静止在桌子上时,它会疯狂旋转。我勒个去?有没有可能我的手机陀螺仪坏了?
【问题讨论】:
标签: iphone