Devicemotion 未更新值



我无法弄清楚为什么滚动、俯仰和偏航值在记录时给出 0.0000。我确信这是我怀念的东西,但我无法弄清楚。

这是代码:

//ViewController.m
#import "ViewController.h"
@interface ViewController (){
}
@property (nonatomic) CMMotionManager *motionManager;
@end
@implementation ViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.motionManager = [[CMMotionManager alloc] init];
    CMDeviceMotion *devMotion = [[CMDeviceMotion alloc]init];
    if ([self.motionManager isDeviceMotionAvailable]) {
        NSLog(@"Device Motion Available");
        [self.motionManager setDeviceMotionUpdateInterval:1.0/30.0];
        // Pull mechanism is used
        [self.motionManager startDeviceMotionUpdates];
    }
    devMotion = self.motionManager.deviceMotion;
    NSLog(@"Roll Pitch and Yaw are %f, %f, %f",devMotion.attitude.roll, devMotion.attitude.pitch, devMotion.attitude.yaw);
}

我已经通过过这个类似的问题:所以问题

请帮助我理解这一点..

谢谢。。


更新的代码:

@implementation ViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.motionManager = [[CMMotionManager alloc] init];
    if ([self.motionManager isDeviceMotionAvailable]) {
        NSLog(@"Device Motion Available");
        [self.motionManager setDeviceMotionUpdateInterval:1.0/30.0];
        // Pull mechanism is used
        [self.motionManager startDeviceMotionUpdates];
    }
    CMDeviceMotion *devMotion = self.motionManager.deviceMotion;
    NSLog(@"*Roll,Pitch and Yaw are %f, %f, %f",devMotion.attitude.roll, devMotion.attitude.pitch, devMotion.attitude.yaw);
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0/30.0 target:self selector:@selector(updateValues:) userInfo:nil repeats:YES];
}
-(void) updateValues:(NSTimer *)timer{
    CMDeviceMotion *currDeviceMotion = self.motionManager.deviceMotion;
    NSLog(@"Roll Pitch and Yaw are %f, %f, %f",currDeviceMotion.attitude.roll, currDeviceMotion.attitude.pitch, currDeviceMotion.attitude.yaw);

}

此代码的很大一部分初始值也为 0.000000 .之后它开始获得值...所以我想startDeviceMotionUpdatesdeviceMotion提供值会有一些延迟。所以看起来我需要弄清楚如何保存第一个非零值。

  • 请注意,您的 devMotion 变量不会立即保存可用值(或任何值),因为CMMotionManager startDeviceMotionUpdates后需要一段时间才能更新 deviceMotion 属性。因此,您应该有某种定期触发并读取该属性的计时器。您链接的文章也做了类似的事情。
    • 只要陀螺仪启动,deviceMotion 属性就会为 null(您只能看到 0.0,因为发送到nil的消息返回零)。
  • 作为旁注:您对 [[CMDeviceMotion alloc] init] 的调用是无用的,因为您随后会用 self.motionManager.deviceMotion 覆盖结果分配给的变量。

相关内容

  • 没有找到相关文章

最新更新