【问题标题】:No gyroscope in iPhone 4?iPhone 4 没有陀螺仪?
【发布时间】:2012-03-08 20:20:55
【问题描述】:

我只想从我的 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;
}

【问题讨论】:

  • 它适合我。检查motionManager 不是nil,并且您实际上是在设备上而不是模拟器上运行。
  • 如何创建motionManager?可以发一下代码吗?
  • 我添加了一些代码,在那里我初始化了motionManager……也许这对我有帮助:)

标签: iphone objective-c cocos2d-iphone gyroscope core-motion


【解决方案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");
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多