我只想从我的 iPhone 4 中获取一些带有 Core Motion 的陀螺仪数据,但总是得到 roll=0、pitch=0 和 yaw=0。经过深入搜索,我意识到
if (motionManager.isDeviceMotionAvailable)
{
[motionManager startDeviceMotionUpdates];
}
返回 false,虽然我在 iPhone 4 上运行它。我必须在设置中启用陀螺仪吗?或者我会做错什么……?
那是我的简化界面:
@interface GameLayer : CCLayer {
CMMotionManager *motionManager;
}
@property (nonatomic, retain) CMMotionManager *motionManager;
@end
这就是我实现motionManager的地方:
- (id) init
{
self=[super init];
if(self)
{
self.motionManager = [[[CMMotionManager alloc] init] autorelease];
motionManager.deviceMotionUpdateInterval = 1.0/60.0;
if (motionManager.isDeviceMotionAvailable)
{
[motionManager startDeviceMotionUpdates];
}
else
{
NSLog(@"No motion captured");
}
[self scheduleUpdate];
}
return self;
}
以及调用它的循环:
- (void) update:(ccTime)delta
{
CMDeviceMotion *currentDeviceMotion = motionManager.deviceMotion;
motionManager.showsDeviceMovementDisplay = YES;
CMAttitude *currentDeviceAttitude = currentDeviceMotion.attitude;
float roll = currentDeviceAttitude.roll;
float pitch = currentDeviceAttitude.pitch;
float yaw = currentDeviceAttitude.yaw;
}