首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >deviceMotion不返回俯仰、滚动或偏航的任何内容

deviceMotion不返回俯仰、滚动或偏航的任何内容
EN

Stack Overflow用户
提问于 2012-03-08 00:35:16
回答 1查看 1.2K关注 0票数 1

我正在尝试使用一些简单的代码来记录我的iPhone 4S的俯仰、侧滚和偏航。它正确地触发了定时器,但俯仰、滚动和偏航的值在控制台中始终显示为0.0000000。这里我漏掉了什么?感谢任何人的帮助!

这是我的.h文件:

代码语言:javascript
复制
@interface ViewController : UIViewController

@property (nonatomic, retain) CMMotionManager *motionManager;
@property(readonly, weak) CMDeviceMotion *deviceMotion;

-(void)doSomething;

@end

下面是我的.m文件:

代码语言:javascript
复制
#import "ViewController.h"

@implementation ViewController

@synthesize motionManager = _motionManager;
@synthesize deviceMotion = _deviceMotion;

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [_motionManager startDeviceMotionUpdates];
    [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(doSomething) userInfo:nil repeats:YES];
}

- (void)doSomething
{
    NSLog(@"Reading Device Motion Updates..");
    _deviceMotion = [_motionManager deviceMotion];
    NSLog(@"Roll = %f, Pitch = %f, Yaw = %f", _deviceMotion.attitude.roll, _deviceMotion.attitude.pitch, _deviceMotion.attitude.yaw);
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    [_motionManager stopDeviceMotionUpdates];
}

@end
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-03-08 00:45:28

您似乎没有为_motionManager分配任何内容。您需要创建此类的一个实例,否则您只是将消息发送到nil

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9605480

复制
相关文章

相似问题

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