2012-03-08 130 views
0

我只是想从我的iPhone 4的Core Motion获取一些陀螺仪数据,但总是得到roll = 0,pitch = 0和yaw = 0。 经过密集的搜索我认识到,iPhone 4没有陀螺仪?

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

IT对我有用。检查'motionManager'不是'nil',你确实在设备上运行,而不是在模拟器上运行。 – sch 2012-03-08 20:45:18

+0

你如何创建你的'motionManager'?你能发布代码吗? – Saphrosit 2012-03-08 20:45:55

+0

我添加了一些代码,我初始化了运动管理器...也许这有助于帮助我:) – clash 2012-03-08 22:45:32

回答

1

使用此代码检查CMMotionManager是否在当前设备上可用。如果这不能初始化CMMotionManager实例,那么您知道您正在没有陀螺仪的设备(如iPhone模拟器)上运行该应用程序:

// check if the motion manager class is available (available since iOS 4.0) 
Class motionManagerClass = NSClassFromString(@"CMMotionManager"); 
if (motionManagerClass) 
{ 
    motionManager = [[motionManagerClass alloc] init]; 
} 
else 
{ 
    NSLog(@"CMMotionManager not available"); 
}