下面程序示範瞭如何經過代碼來獲取加速度數據、陀螺儀數據、磁場數據。新建一個SingleView Application,該項目包含一個應用程序委託類,一個視圖控制器類和Main.storyboardatom
界面設計文件。線程
打開界面設計文件,向其中拖入3個UILbale,並將它們的lines屬性設爲「0」,這個3個UILabel用於顯示程序獲取的加速度數據、陀螺儀數據,磁場數據。爲了在程序中訪問它們,須要將它們綁定到視圖控制器的accelerometerLabel\gyroLabel\magnetometerLabel這3個IBOutlet屬性。設計
下面是該示例視圖控制器代碼:orm
1、基於代碼塊方式獲取對象
@interface ViewController () { } @property (weak, nonatomic) IBOutlet UILabel *gyroLabel; @property (weak, nonatomic) IBOutlet UILabel *accelerometerLabel; @property (weak, nonatomic) IBOutlet UILabel *magnetometerLabel; @property (strong,nonatomic)CMMotionManager * motionManager; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. //CFMotionmangaer對象 self.motionManager = [[CMMotionManager alloc]init]; NSOperationQueue * queue = [[NSOperationQueue alloc] init]; //若是CMMotionManager支持獲取加速度數據 if (self.motionManager.accelerometerAvailable) { //設置CMMotionManager的加速度數據更新頻率爲0.1秒 self.motionManager.accelerometerUpdateInterval = 0.1; //使用代碼塊開始獲取加速度數據 [self.motionManager startAccelerometerUpdatesToQueue:queue withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) { NSString * labelText; //若是發生了錯誤,error不爲空 if (error) { //中止獲取加速度數據 [self.motionManager stopAccelerometerUpdates]; labelText = [NSString stringWithFormat:@"獲取加速度shuu出現錯誤:%@",error]; } else { FKbarllView * barllview = [[FKbarllView alloc] init]; barllview.acceleration = accelerometerData.acceleration; //分別獲取系統在X,Y,Z軸上的加速度數據 labelText = [NSString stringWithFormat:@"加速度爲\n-----\nX軸:%+.2f\nY軸:%+.2f\nZ軸:%.2f", accelerometerData.acceleration.x,accelerometerData.acceleration.y,accelerometerData.acceleration.z]; } //在主線程中更新accelerometerLabel的文本,顯示加速度數據 [self.accelerometerLabel performSelectorOnMainThread:@selector(setText:) withObject:labelText waitUntilDone:NO]; }]; } else{ NSLog(@"該設備不支持獲取加速度數據!"); } //若是CMMotionManager支持獲取陀螺儀數據 if (self.motionManager.gyroAvailable) { //設置CMMotionManager的陀螺儀數據更新頻率爲0.1秒 [self.motionManager startGyroUpdatesToQueue:queue withHandler:^(CMGyroData *gyroData, NSError *error) { NSString * labelText; //若是發生了錯誤,error不爲空 if(error) { //中止獲取陀螺儀數據 [self.motionManager stopGyroUpdates]; labelText = [NSString stringWithFormat:@"獲取陀螺儀數據出現錯誤;%@",error]; } else { //分別獲取設備燒X,Y,Z軸的轉速 labelText = [NSString stringWithFormat:@"繞各軸的轉速爲\n-----\nX軸:%+.2f\nY軸:%+.2f\nZ軸:%.2f", gyroData.rotationRate.x, gyroData.rotationRate.y, gyroData.rotationRate.z]; } //在主線程中更新gyroLabel的文本,顯示繞各軸的轉速 [self.gyroLabel performSelectorOnMainThread:@selector(setText:) withObject:labelText waitUntilDone:NO]; }]; }else { NSLog(@"該設備不支持獲取陀螺儀數據!"); } //若是CMMotionManager支持獲取磁場數據 if (self.motionManager.magnetometerAvailable) { //設置CMMot的磁場數據更新頻率爲0.1秒 self.motionManager.magnetometerUpdateInterval = 0.1f; [self.motionManager startMagnetometerUpdatesToQueue:queue withHandler:^(CMMagnetometerData * magnetometerData, NSError *error) { NSString * labelText; if (error) { //中止獲取數據 [self.motionManager stopMagnetometerUpdates]; labelText = [NSString stringWithFormat:@"獲取磁場數據出現錯誤;%@",error]; } else{ labelText = [NSString stringWithFormat:@"磁場數據爲\n-----\nX軸:%+.2f\nY軸:%+.2f\nZ軸:%.2f", magnetometerData.magneticField.x, magnetometerData.magneticField.y, magnetometerData.magneticField.z]; } //在主線程中更新magnetometerLabel的文本,顯示磁場數據 [self.magnetometerLabel performSelectorOnMainThread:@selector(setText:) withObject:labelText waitUntilDone:NO]; }]; } else { NSLog(@"該設備不支持獲取磁場數據!"); } }
下面程序示範瞭如何經過主動請求來獲取加速度數據、陀螺儀數據、磁場數據。新建一個SingleView Application,該項目包含一個應用程序委託類,一個視圖控制器類和Main.storyboardblog
界面設計文件。get
打 開界面設計文件,向其中拖入3個UILbale,並將它們的lines屬性設爲「0」,這個3個UILabel用於顯示程序獲取的加速度數據、陀螺儀數 據,磁場數據。爲了在程序中訪問它們,須要將它們綁定到視圖控制器的accelerometerLabel\gyroLabel \magnetometerLabel這3個IBOutlet屬性。string
下面是該示例視圖控制器代碼:it
2、主動請求獲取io
@interface ViewController () { NSTimer * updateTimer; } @property (weak, nonatomic) IBOutlet UILabel *accelerometerLabel; @property (weak, nonatomic) IBOutlet UILabel *gyroLabel; @property (weak, nonatomic) IBOutlet UILabel *magnetometerLabel; @property (strong,nonatomic) CMMotionManager* motionMaager; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. //建立CMMotionManager對象 self.motionMaager = [[CMMotionManager alloc] init]; //若是CMMotionManager支持獲取加速度數據 if (self.motionMaager.accelerometerAvailable) { [self.motionMaager startAccelerometerUpdates]; } else { NSLog(@"該設備不支持獲取加速度數據!"); } //若是CMMotionManager支持獲取陀螺儀數據 if (self.motionMaager.gyroAvailable) { [self.motionMaager startGyroUpdates]; } else { NSLog(@"該設備不支持獲取陀螺儀數據!"); } //若是CMMotionManager支持獲取磁場數據 if (self.motionMaager.magnetometerAvailable) { [self.motionMaager startMagnetometerUpdates]; } else { NSLog(@"該設備不支持獲取磁場數據!"); } } -(void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; //啓動定時器來週期性輪詢加速度數據,陀螺儀數據,磁場數據 updateTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(updateDisplay) userInfo:nil repeats:YES]; } -(void)updateDisplay { //若是CMMotionManager的加速度數據可用 if (self.motionMaager.accelerometerAvailable) { //主動請求獲取加速度數據 CMAccelerometerData * accelerometerData = self.motionMaager.accelerometerData; self.accelerometerLabel.text = [NSString stringWithFormat:@"加速度爲\n-\nX軸:%+.2f\nY軸:%+.2f\nZ軸:%+.2f",accelerometerData.acceleration.x, accelerometerData.acceleration.y ,accelerometerData.acceleration.z]; } //若是CMMotionManager的陀螺儀數據可用 if (self.motionMaager.gyroAvailable) { //主動請求獲取加速度數據 CMGyroData * gyroData = self.motionMaager.gyroData; // gyroData.rotationRate self.gyroLabel.text = [NSString stringWithFormat:@"陀螺儀數據爲\n-\nX軸:%+.2f\nY軸:%+.2f\nZ軸:%+.2f",gyroData.rotationRate.x, gyroData.rotationRate.y ,gyroData.rotationRate.z]; } //若是CMMotionManager的磁場數據可用 if (self.motionMaager.magnetometerAvailable) { //主動請求獲取加速度數據 CMMagnetometerData * magnetometerData = self.motionMaager.magnetometerData; self.magnetometerLabel.text = [NSString stringWithFormat:@"加速度爲\n-\nX軸:%+.2f\nY軸:%+.2f\nZ軸:%+.2f",magnetometerData.magneticField.x, magnetometerData.magneticField.y ,magnetometerData.magneticField.z]; } }
,在真機上運行這兩個程序,將能夠看到運行結果