首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Devicemotion没有更新值

Devicemotion没有更新值
EN

Stack Overflow用户
提问于 2013-10-29 10:24:02
回答 1查看 1.9K关注 0票数 2

我不知道为什么当记录时,滚动、俯仰和偏航值为0.0000。我确信这是我错过的一件事,但我想不出。

这是代码:

代码语言:javascript
复制
//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);

}

我已经研究过类似的问题了:SO Question

请帮我理解一下..。

谢谢..

更新代码:

代码语言:javascript
复制
@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。从那以后它开始获得价值..。因此,我想startDeviceMotionUpdates在向deviceMotion提供值方面存在一些延迟。所以看起来我需要弄清楚如何保存第一个非零值。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-10-29 10:34:02

  • 请注意,devMotion变量不会立即保存可用的值(或任何值),因为CMMotionManager需要一段时间才能在startDeviceMotionUpdates之后更新deviceMotion属性。因此,您应该有某种定期触发并读取该属性的计时器。你的链接文章做了一些类似的事情。
    • 只要陀螺仪启动,deviceMotion属性将为null (您只看到0.0,因为nil的消息返回零)。

  • 作为一个sidenote:您对[[CMDeviceMotion alloc] init]的调用是无用的,因为您随后用self.motionManager.deviceMotion重写了结果分配给的变量。
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19655466

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档