下面程序示范了如何通过代码来获取加速度数据、陀螺仪数据、磁场数据。新建一个SingleView Application,该项目包含一个应用程序委托类,一个视图控制器类和Main.storyboard
界面设计文件。
打开界面设计文件,向其中拖入3个UILbale,并将它们的lines属性设为“0”,这个3个UILabel用于显示程序获取的加速度数据、陀螺仪数据,磁场数据。为了在程序中访问它们,需要将它们绑定到视图控制器的accelerometerLabelgyroLabelmagnetometerLabel这3个IBOutlet属性。
下面是该示例视图控制器代码:
一、基于代码块方式获取
@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:@"加速度为 ----- X轴:%+.2f Y轴:%+.2f Z轴:%.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:@"绕各轴的转速为 ----- X轴:%+.2f Y轴:%+.2f Z轴:%.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:@"磁场数据为 ----- X轴:%+.2f Y轴:%+.2f Z轴:%.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.storyboard
界面设计文件。
打 开界面设计文件,向其中拖入3个UILbale,并将它们的lines属性设为“0”,这个3个UILabel用于显示程序获取的加速度数据、陀螺仪数 据,磁场数据。为了在程序中访问它们,需要将它们绑定到视图控制器的accelerometerLabelgyroLabel magnetometerLabel这3个IBOutlet属性。
下面是该示例视图控制器代码:
二、主动请求获取
@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:@"加速度为 - X轴:%+.2f Y轴:%+.2f Z轴:%+.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:@"陀螺仪数据为 - X轴:%+.2f Y轴:%+.2f Z轴:%+.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:@"加速度为 - X轴:%+.2f Y轴:%+.2f Z轴:%+.2f",magnetometerData.magneticField.x, magnetometerData.magneticField.y ,magnetometerData.magneticField.z]; } }
,在真机上运行这两个程序,将可以看到运行结果