本文主要是介绍加速计_陀螺仪_磁力计,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
在Build Phases 下的Link Binary With Libraries (1 item)中加入 CoreMotion.framework文件包
#import "ViewController.h"
#import <CoreMotion/CoreMotion.h>
@interface ViewController ()
@property(nonatomic, strong)UILabel *CMAlabel; /**< 加速度 */
@property(nonatomic, strong)UILabel *CMGlabel; /**< 陀螺仪 */
@property(nonatomic, strong)UILabel *CMMlabel; /**< 磁力计 */
@property(nonatomic, strong)CMMotionManager *motionManager;
@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad];self.CMAlabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 50,375, 150)];self.CMAlabel.numberOfLines = 5;self.CMAlabel.backgroundColor = [UIColor yellowColor];[self.view addSubview:self.CMAlabel];// 创建CMMontionManagerself.motionManager = [CMMotionManager new];// 创建子线程来更新数据NSOperationQueue *queue = [NSOperation new];if (self.motionManager.accelerometerAvailable) {// 设置CMMotionManager的加速度读更新频率self.motionManager.accelerometerUpdateInterval = 0.1;// 使用代码块开始获取加速度的数据[self.motionManager startAccelerometerUpdatesToQueue:queue withHandler:^(CMAccelerometerData * accelerometerData, NSError * error) {NSString *labeltext;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];}// 在主线程更新文本[self.CMAlabel performSelectorOnMainThread:@selector(setText:) withObject:labeltext waitUntilDone:NO];}];}else{NSLog(@"该设备不支持获取加速度数据");}// 陀螺仪self.CMGlabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 220, 375, 150)];self.CMGlabel.numberOfLines = 5;self.CMGlabel.backgroudColor = [UIColor yellowColor];[self.view addSubview:self.CMGlabel];// CMMotionManager 支持获取陀螺仪数据if (self.motionManager.gyroAvailable) {// 设备获取陀螺仪数据的更新频率;self.motionManager.gyroUpdateInterval = 0.1;// 使用代码块开始获取陀螺仪数据[self.motionManager startGyroUpdatesToQueue:queue withHandler:^(CMGyroData *gyroData, NSError *error) {NSString *labeltext;if (error) {[self.motionManager stopGyroUpdates];labeltext = [NSString stringWithFormat:@"获取陀螺仪数据出现错误%@", error];} else {labeltext = [NSString stringWithFormat:@"绕各轴转速为\n-------------\nX轴%+.2f \ nY轴%+.2f \nZ轴%+.2f", gyroData.rotationRate.x, gyroData.rotationRate.y, gyroData.rotationRate.z];}[self.CMGlabel performSelectorOnMainThread:@selector(setText:) withObject:labeltext waitUntilDone:ON];}];} else {NSLog(@"该设备不支持获取陀螺仪数据");}// 磁力计
self.CMMlabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 390, 375, 150)];
[self.view addSubview:self.CMMlabel];
self.CMMlabel.numberOfLines = 5;
self.CMMlabel.backgroundColor = [UIColor yellowColor];
if (self.motionManager.magnetometerAvailable) {self.motionManager.magnetometerUpdateInterval = 0.1;[self.motionManager startMagnetometerUpdatesToQueue:queue withHandler:^(CMMagnetometerData * magnetometerData, NSError *error) {NSString *labeltext;if (error) {[self.motionManager stopMagmetometerUpdates];labeltext = [NSString stringWithFormat:@"获取磁场数据出现错误%@", error];}else{labeltext = [NSString stringWithFormat:@"磁场数据\n------------\nX轴%+.2f \nY轴%+.2f \nZ轴%+.2f", magnetometerData.magneticField.x, magnetometerData.magneticField.y, magnetometerData.magneticField.z];}[self.CMMlabel performSelectorOnMainThread:@selector(setText:) withObject:labeltext waitUntilDone:NO];}];
}else{NSLog(@"该设备不支持获取磁场数据");
}}
这篇关于加速计_陀螺仪_磁力计的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!