我无法弄清楚为什么滚动,俯仰和偏航值在记录时给出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);
}
我已经通过这个类似的问题:SO Question
请帮我理解这个..
谢谢..
更新代码:
@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
提供值时会有一些延迟。所以看起来我需要弄清楚如何保存第一个非零值。
答案 0 :(得分:0)
devMotion
变量不会立即保留可用值(或根本不保留任何值),因为CMMotionManager
需要一段时间才能在deviceMotion
之后更新startDeviceMotionUpdates
属性。所以你应该有一些定期触发并读取该属性的计时器。您链接的文章做了类似的事情。
deviceMotion
属性将为空(您只看到0.0,因为到nil
的消息返回零)。[[CMDeviceMotion alloc] init]
的调用毫无用处,因为您覆盖了使用self.motionManager.deviceMotion
分配结果的变量。