2012-03-08 1 views
0

Я просто хочу получить данные гироскопа с Core Motion с моего iPhone 4, но всегда получаю roll = 0, pitch = 0 и yaw = 0. После интенсивного поиска я узнал, чтоНет гироскопа в iPhone 4?

if (motionManager.isDeviceMotionAvailable) 
{ 
    [motionManager startDeviceMotionUpdates]; 
} 

возвращает ложь, хотя я запустить его на 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

+0

Как вы создаете свой 'motionManager'? Можете ли вы опубликовать код? – Saphrosit

+0

Я добавил код, где я инициализирую motionManager ... Может быть, это помогает мне :) – clash

ответ

1

Используйте этот код проверьте, доступен ли CMMotionManager на текущем устройстве. Если это не удается инициализировать экземпляр CMMotionManager, вы знаете, вы запускаете приложение на устройстве без гироскопа, таких как iPhone Simulator:

// 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"); 
} 
Смежные вопросы