2012-03-08 147 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"); 
}