我無法弄清楚爲什麼滾動,俯仰和偏航值在登錄時給出了0.0000 ..我確定這是一個我想念的東西,但我無法弄清楚它..Devicemotion沒有更新值
這是代碼:
//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的很大一部分。之後,它開始變得值...所以我想有是向deviceMotion
提供值一些延遲startDeviceMotionUpdates
。所以看起來我需要弄清楚如何保存第一個非零值。
devMotion
變量將不會舉行可用值(或全部)馬上爲
CMMotionManager
你更新的代碼工作,是在我的iPhone 4S。您是否在使用沒有陀螺儀(或模擬器)的設備? –
我也在使用iPhone 4s,現在它正在工作..請閱讀下面的代碼聲明。 :)我說的是,初始值是零。過了一段時間,它正在記錄非零值。但我想立即得到的值,這似乎是困難的,因爲devMotion變量將不會舉行可用值(或全部)馬上爲CMMotionManager需要一段時間startDeviceMotionUpdates後更新deviceMotion屬性,如你所說。 – iSeeker
當陀螺儀啓動時,我認爲'currDeviceMotion'本身就是'nil',所以你可以測試一下。 –