我不知道为什么当记录时,滚动、俯仰和偏航值为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提供值方面存在一些延迟。所以看起来我需要弄清楚如何保存第一个非零值。
发布于 2013-10-29 10:34:02
devMotion变量不会立即保存可用的值(或任何值),因为CMMotionManager需要一段时间才能在startDeviceMotionUpdates之后更新deviceMotion属性。因此,您应该有某种定期触发并读取该属性的计时器。你的链接文章做了一些类似的事情。deviceMotion属性将为null (您只看到0.0,因为nil的消息返回零)。
[[CMDeviceMotion alloc] init]的调用是无用的,因为您随后用self.motionManager.deviceMotion重写了结果分配给的变量。https://stackoverflow.com/questions/19655466
复制相似问题