加速计_陀螺仪_磁力计

在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];
   // 创建CMMontionManager
   self.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 stopAccelerometer
   Updates];
   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(@"该设备不支持获取磁场数据");
}

  }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值