获取加速度数据,陀螺仪数据,磁场数据的两种方式-陈鹏
2015-05-15 15:27
302 查看
下面程序示范了如何通过代码来获取加速度数据、陀螺仪数据、磁场数据。新建一个SingleView Application,该项目包含一个应用程序委托类,一个视图控制器类和Main.storyboard
界面设计文件。
打开界面设计文件,向其中拖入3个UILbale,并将它们的lines属性设为“0”,这个3个UILabel用于显示程序获取的加速度数据、陀螺仪数据,磁场数据。为了在程序中访问它们,需要将它们绑定到视图控制器的accelerometerLabel\gyroLabel\magnetometerLabel这3个IBOutlet属性。
下面是该示例视图控制器代码:
一、基于代码块方式获取
下面程序示范了如何通过主动请求来获取加速度数据、陀螺仪数据、磁场数据。新建一个SingleView Application,该项目包含一个应用程序委托类,一个视图控制器类和Main.storyboard
界面设计文件。
打 开界面设计文件,向其中拖入3个UILbale,并将它们的lines属性设为“0”,这个3个UILabel用于显示程序获取的加速度数据、陀螺仪数 据,磁场数据。为了在程序中访问它们,需要将它们绑定到视图控制器的accelerometerLabel\gyroLabel \magnetometerLabel这3个IBOutlet属性。
下面是该示例视图控制器代码:
二、主动请求获取
,在真机上运行这两个程序,将可以看到运行结果
界面设计文件。
打开界面设计文件,向其中拖入3个UILbale,并将它们的lines属性设为“0”,这个3个UILabel用于显示程序获取的加速度数据、陀螺仪数据,磁场数据。为了在程序中访问它们,需要将它们绑定到视图控制器的accelerometerLabel\gyroLabel\magnetometerLabel这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:@"加速度为\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.storyboard
界面设计文件。
打 开界面设计文件,向其中拖入3个UILbale,并将它们的lines属性设为“0”,这个3个UILabel用于显示程序获取的加速度数据、陀螺仪数 据,磁场数据。为了在程序中访问它们,需要将它们绑定到视图控制器的accelerometerLabel\gyroLabel \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:@"加速度为\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]; } }
,在真机上运行这两个程序,将可以看到运行结果
相关文章推荐
- Android之从网络中获取数据并返回客户端的两种方式:XML格式返回与Json格式返回
- 在线程中使用HttpClicent获取网络数据(get和post两种方式)
- 【Android】传感器(线性加速度、陀螺仪、磁场)信号实时获取,实时监测指定wifi、蓝牙信号强度
- .net 调用SAP RFC函数获取数据的两种方式
- Spark Streaming获取kafka数据的两种方式
- 通过ashx获取JSON数据的两种方式
- 通过ashx获取JSON数据的两种方式
- ajax获取数据的两种方式
- Spark-Streaming获取kafka数据的两种方式:Receiver与Direct的方式
- XZ_CoreMotion框架获取数据的两种方式
- iOS 通过URL网络获取XML数据的两种方式
- logstash快速入门 (这篇文章很不错 ) | 两种方式往logstash传输数据实例:Apache 日志(从文件获取)、Syslog方式
- extjs获取和传递数据的两种方式
- extjs获取和传递数据的两种方式
- iOS 通过URL网络获取XML数据的两种方式
- bootstrap table 获取数据的两种方式
- iOS 通过URL网络获取XML数据的两种方式
- swfit3.0 两种方式获取post数据
- Android 下陀螺仪、加速度和磁场传感器数据定义
- Android两种方式获取网络数据