Нет гироскопа в iPhone 4? - PullRequest
       35

Нет гироскопа в iPhone 4?

0 голосов
/ 09 марта 2012

Я просто хочу получить данные гироскопа с Core Motion с моего iPhone 4, но всегда получаю 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;
}

1 Ответ

1 голос
/ 10 марта 2012

Используйте этот код, чтобы проверить, доступен ли 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");
}
...