iOS CoreMotion实现设备运动加速度计陀螺仪

2020-01-21 03:10:15王旭

代码示例

Push方式获取数据

加速度计


CMMotionManager *manager = [[CMMotionManager alloc] init];
if ([manager isAccelerometerAvailable] && ![manager isAccelerometerActive]){
  NSOperationQueue *queue = [[NSOperationQueue alloc] init];  
  manager.accelerometerUpdateInterval = 0.1;//设置信息更新频率(0.1s获取一次)
  [manager startAccelerometerUpdatesToQueue:queue
                 withHandler:^(CMAccelerometerData *accelerometerData, NSError *error)
   {
     CMAcceleration acceleration = accelerometerData.acceleration;
     
     NSLog(@"x = %.04f", acceleration.x);
     NSLog(@"y = %.04f", acceleration.y);
     NSLog(@"z = %.04f", acceleration.z);

   }];
}

陀螺仪


CMMotionManager *manager = [[CMMotionManager alloc] init];  
  if ([manager isGyroAvailable] && ![manager isGyroActive]){    
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    manager.gyroUpdateInterval = 0.1;//设置信息更新频率(0.1s获取一次)
    [manager startGyroUpdatesToQueue:queue
               withHandler:^(CMGyroData *gyroData, NSError *error)
     {
      CMRotationRate rotationrate = gyroData.rotationRate;
      NSLog(@"x = %.04f", rotationRate.x);
      NSLog(@"y = %.04f", rotationRate.y);
      NSLog(@"z = %.04f", rotationRate.z);
     }];
  }

Pull方式获取数据


#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height
@interface ViewController ()
@property (strong, nonatomic) CMMotionManager *motionManager;
//UI
@property (strong, nonatomic) UIButton *starButton;       //启动 MotionManager
@property (strong, nonatomic) UIButton *pullButton;       //拉取数据
@property (strong, nonatomic) UIButton *stopButton;       //停止 MotionManager
@end

@implementation ViewController
#pragma mark - 懒加载
- (CMMotionManager *)motionManager{  
  if (!_motionManager) {    
    _motionManager = [[CMMotionManager alloc] init];
    _motionManager.accelerometerUpdateInterval = 0.1;
    _motionManager.gyroUpdateInterval = 0.1;
  }
  return _motionManager;
}


- (UIButton *)starButton{  
  if (!_starButton) {    
    _starButton = [[UIButton alloc]initWithFrame:CGRectMake(50, 100, 50, 50)];
    [_starButton setTitle:@"启动" forState:UIControlStateNormal];
    [_starButton addTarget:self action:@selector(startMotion) forControlEvents:UIControlEventTouchUpInside];
  }
  return _starButton;
}

- (UIButton *)pullButton{  
  if (!_pullButton) {    
    _pullButton = [[UIButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/2-50, 100, 100, 50)];
    [_pullButton setTitle:@"拉取数据" forState:UIControlStateNormal];
    [_pullButton addTarget:self action:@selector(pullMotionData) forControlEvents:UIControlEventTouchUpInside];
  }
  return _pullButton;
}

- (UIButton *)stopButton{  
  if (!_stopButton) {    
    _stopButton = [[UIButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH-100, 100, 50, 50)];
    [_stopButton setTitle:@"停止" forState:UIControlStateNormal];
    [_stopButton addTarget:self action:@selector(stopMotion) forControlEvents:UIControlEventTouchUpInside];
  }
  return _stopButton;
}


#pragma mark - 生命周期处理
- (void)viewDidLoad {
  [self.view addSubview:self.starButton];
  [self.view addSubview:self.pullButton];
  [self.view addSubview:self.stopButton];
}

#pragma mark - Pull
- (void)startMotion{
  if (self.motionManager.isAccelerometerActive == NO) {
    [self.motionManager startAccelerometerUpdates];
  }
  if (self.motionManager.isGyroActive == NO){
    [self.motionManager startGyroUpdates];
  }
}

- (void)pullMotionData{  
  //陀螺仪拉取数据
  CMGyroData *gyroData = [self.motionManager gyroData];
  CMRotationRate rotationrate = gyroData.rotationRate;
  NSLog(@"x = %.04f", rotationRate.x);
  NSLog(@"y = %.04f", rotationRate.y);
  NSLog(@"z = %.04f", rotationRate.z);
  
  //加速度计拉取数据
  CMAccelerometerData *data = [self.motionManager accelerometerData];
  CMAcceleration acceleration = data.acceleration;
  NSLog(@"x = %.04f", acceleration.x);
  NSLog(@"y = %.04f", acceleration.y);
  NSLog(@"z = %.04f", acceleration.z);
}

- (void)stopMotion{  
  //陀螺仪
  if (self.motionManager.isGyroActive == YES) {
    [self.motionManager stopGyroUpdates];
  }
  //加速度计
  if (self.motionManager.isAccelerometerActive == YES) {
    [self.motionManager stopAccelerometerUpdates];
  }
}
@end