iOS应用程序获取加速度数据、陀螺仪数据、磁场数据有如下两种方式:
以基于代码块的方式获取加速度数据、陀螺仪数据、磁场数据,这种方式适用于普通的、实时性要求较低的应用。
以周期性主动请求的方式获取加速度数据、陀螺仪数据、磁场数据,这种方式适用于实时性要求较高,或者程序要随时获取这些数据的应用,比如哟啊感知加速度数据、陀螺仪数据的游戏等。
2、基于代码块方式获取加速度数据、陀螺仪数据、磁场数据
如果使用基于代码块的方式来获取加速计数据,请按如下步骤进行:
- ①、创建CMMotionManager对象;
- ②、为CMMotionManager设置获取加速度数据。陀螺仪数据、磁场数据的频率,通常就是设置xxxUpdateInterval属性,该属性值的单位是秒。其中xxx代表accelerometer(加速度)、gyro(陀螺仪)和magnetometer(磁场)中任意一个。
- ③、调用CMMotionManager对象的startXxxUpdatesToQueue:queue withHandler:方法来周期性地获取加速度数据、陀螺仪数据、磁场数据。其中Xxx同样代表Accelerometer(加速度)、Gyro(陀螺仪)、Magnetometer(磁场)中任意一个。
- ④、如果程序出现错误,或者程序想终止获取这些数据,则可调用stopXxxUpdates方法停止获取。
例如:
加入头文件#import < CoreMotion/CoreMotion.h >
- (void)viewDidLoad {
[super viewDidLoad];
//创建CMMotionManager对象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:@"获取加速度数据出现错误:%@",error];
}else{
//分别获取系统在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{
[self.accelerometerLabel performSelectorOnMainThread:@selector(setText:) withObject:@"该设备不支持获取加速度数据!" waitUntilDone:NO];
NSLog(@"该设备不支持获取加速度数据!");
}
//如果CMMotionManager的支持获取陀螺仪数据if (self.motionManager.gyroAvailable) {
//设置CMMOtionManager的陀螺仪数据更新频率为0.1;self.motionManager.gyroUpdateInterval = 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{
[self.gyroLabel performSelectorOnMainThread:@selector(setText:) withObject:@"该设备不支持获取陀螺仪数据!" waitUntilDone:NO];
}
//如果CMMotionManager的支持获取磁场数据if (self.motionManager.magnetometerAvailable) {
//设置CMMotionManager的磁场数据更新频率为0.1秒self.motionManager.magnetometerUpdateInterval = 0.1;
[self.motionManager startMagnetometerUpdatesToQueue:queue withHandler:^(CMMagnetometerData *magnetometerData, NSError *error) {
NSString* labelText;
//如果发生了错误 error不为空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{
[self.magnetometerLabel performSelectorOnMainThread:@selector(setText:) withObject:@"该设备不支持获取磁场数据!" waitUntilDone:NO];
}
}
3、主动请求获取加速度数据、陀螺仪数据、磁场数据
如果使用主动请求的方式来获取加速计数据,请按如下步骤进行:
- ①、创建CMMotionManager对象;
- ②、调用CMMotionManager对象的startXxxUpdates方法开始更新加速度数据、陀螺仪数据、磁场数据。其中Xxx代表Accelerometer(加速度)、Gyro(陀螺仪)和Magnetometer(磁场)中的任何一个。
- ③、使用定时器、子线程或其他任意的额可以周期性轮询机制通过CMMotionManager对象加速度数据、陀螺仪数据、磁场数据。
- ④、如果程序出现错误,或者程序想终止获取这些数据,则可调用stopXxxUpdates方法停止获取。
加入头文件 #import < CoreMotion/CoreMotion.h >
#import #import "ViewController.h"@interfaceViewController ()
{
NSTimer* updateTimer;
}
@property (strong ,nonatomic) CMMotionManager* motionManager;
@end@implementationViewController
- (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.motionManager.accelerometerAvailable) {
//主动请求获取加速度数据
CMAccelerometerData* accelerometerData = self.motionManager.accelerometerData;
self.accelerometerLabel.text = [NSString stringWithFormat:@"加速度为\n---------\nX轴:%+2.f\nY轴:%+2.f\nZ轴:%+2.f",accelerometerData.acceleration.x,accelerometerData.acceleration.y,accelerometerData.acceleration.z];
}
if (self.motionManager.magnetometerAvailable) {
//如果CMMotionManager的陀螺仪数据可用
CMGyroData* gyroData = self.motionManager.gyroData;
self.gyroLabel.text = [NSString stringWithFormat:@"绕个轴的转速为\n--------/nX轴:%+2.f\nY轴:%+.2f\nZ轴:%+.2f",gyroData.rotationRate.x,gyroData.rotationRate.y,gyroData.rotationRate.z];
}
//如果CMMotionManager的磁场数据可用if (self.motionManager.magnetometerAvailable) {
//主动请求获取磁场数据
CMMagnetometerData* magnetometerData = self.motionManager.magnetometerData;
self.magnetometerLabel.text = [NSString stringWithFormat:@"磁场数据为\n--------/nX轴:%+2.f\nY轴:%+.2f\nZ轴:%+.2f",magnetometerData.magneticField.x,magnetometerData.magneticField.y,magnetometerData.magneticField.z];
}
}
- (void)viewDidLoad {
[super viewDidLoad];
//创建CMMotionManager对象self.motionManager = [[CMMotionManager alloc]init];
//如果CMMotionManager的支持获取加速度数据if (self.motionManager.accelerometerAvailable) {
[self.motionManager startAccelerometerUpdates];
}else{
NSLog(@"该设备不支持获取加速度数据!");
}
//如果CMMotionManager的支持获取陀螺仪数据if (self.motionManager.gyroAvailable) {
[self.motionManager startGyroUpdates];
}else{
NSLog(@"该设备不支持获取陀螺仪数据!");
}
//如果CMMotionManager的支持获取磁场数据if (self.motionManager.magnetometerAvailable) {
[self.motionManager startMagnetometerUpdates];
}else{
NSLog(@"该设备不支持获取磁场数据!");
}
}
@end
实例:怪物足球
详情请看代码
二、感知设备移动
除了获取加速度数据、陀螺仪数据和磁场数据之外,CMMotionManager还可以用于感知设备移动数据。与前获取加速度数据、陀螺仪数据、磁场数据的方式完全相同,程序可通过如下两种方式来感知设备的移动数据。
使用基于代码的方式获取设备移动数据;
使用哪个周期性主动请求的方式获取设备移动数据;
获取设备移动数据时,CMMotionManager将会返回一个CMDeviceMotion对象,该对象包含如下属性:
- attitude:该属性返回设备的方位信息。该属性的分那会至是一个CMAttitude类型的对象,该对象包含roll、pitch、yaw3个欧拉角的值,通过这3个值即可获取该设备的空间方位;
- rotationRate:该属性返回原始的陀螺仪信息,该属性值为CMRotationRate结构体变量,该属性值基本等同于前面介绍的陀螺仪数据;
- gravity:该属性返回地球重力对该设备在X、Y、Z轴上施加的重力加速度;
- userAcceleration:该属性返回用户外力对该设备在X、Y、Z轴上施加的重力加速度;
- magneticField:该属性返回校准后的磁场信息。该属性值是一个CMCalibratedMagneticField结构体变量。CMCalibratedMagneticField类型的变量包括field和accuracy两个字段,其中field代表X、Y、Z、轴上的磁场强度,accuracy则代表磁场强度的精度;
注:attitude属性是一个CMAttitude类型的变量,这种类型的变量用于表示该设备的空间方位。其中roll、pitch、yaw这3个角度的意义如下: