亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

iOS CoreMotion實現(xiàn)設備運動加速度計陀螺儀

 更新時間:2017年12月19日 13:47:04   作者:藍畔湖光  
這篇文章主要介紹了iOS CoreMotion實現(xiàn)設備運動加速度計陀螺儀,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

用于處理加速度計,陀螺儀,計步器和與環(huán)境有關的事件。

Core Motion框架從iOS設備的板載硬件(包括加速計,陀螺儀,計步器,磁力計和氣壓計)報告與運動和環(huán)境有關的數(shù)據(jù)。您可以使用此框架訪問硬件生成的數(shù)據(jù),以便您可以在應用程序中使用它。例如,游戲可能使用加速度計和陀螺儀數(shù)據(jù)來控制屏幕上的游戲行為。 這個框架的許多服務都可以訪問硬件記錄的原始值和這些值的處理版本。處理后的值不包括第三方因素對該數(shù)據(jù)的造成不利影響的情況。例如,處理的加速度計值僅反映由用戶引起的加速度,而不是由重力引起的加速度。

在iOS 10.0或之后版本的iOS應用程序必須在其Info.plist文件中包含使用說明Key的描述,以告知用戶獲取所需的數(shù)據(jù)類型及獲取數(shù)據(jù)類型的目的。未能包含這些Key會導致應用程序崩潰。特別是訪問運動和健身數(shù)據(jù)時,必須聲明 NSMotionUsageDescription

設備運動

設備運動服務提供了一種簡單的方法,讓您獲取應用程序的運動相關數(shù)據(jù)。原始的加速度計和陀螺儀數(shù)據(jù)需要處理,以消除其他因素(如重力)的偏差。設備運動服務為您處理這些數(shù)據(jù),為您提供可以立即使用的精確數(shù)據(jù)。例如,此服務為用戶啟動的加速度和重力引起的加速度提供單獨的值。因此,此服務可讓您專注于使用數(shù)據(jù)來操縱您的內(nèi)容,而不是處理該數(shù)據(jù)。 設備運動服務使用可用的硬件來生成CMDeviceMotion對象,其中包含以下信息:

  1. 設備在三維空間中相對于參考框架的方向(或姿態(tài))
  2. 無偏的旋轉(zhuǎn)速度
  3. 當前重力矢量
  4. 用戶生成的加速度矢量(無重力)
  5. 當前的磁場矢量

加速計

 

該圖為,加速度計沿x,y和z軸的速度變化

加速度計測量沿一個軸的速度變化。 所有的iOS設備都有一個三軸加速度計,它在圖1所示的三個軸中的每一個軸上提供加速度值。加速度計報告的值以重力加速度的增量進行測量,值1.0代表9.8米的加速度 每秒(每秒)在給定的方向。 取決于加速度的方向,加速度值可能是正值或負值。

陀螺儀

 

該圖為,旋轉(zhuǎn)反向速率對陀螺儀繞x,y和z軸的影響變化

陀螺儀測量設備圍繞空間軸旋轉(zhuǎn)的速率。 許多iOS設備都有一個三軸陀螺儀,它可以在圖1所示的三個軸中的每一個軸上提供旋轉(zhuǎn)值。旋轉(zhuǎn)值以給定軸每秒的弧度為單位進行測量。 根據(jù)旋轉(zhuǎn)的方向,旋轉(zhuǎn)值可以是正值或負值。

代碼示例

Push方式獲取數(shù)據(jù)

加速度計

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方式獲取數(shù)據(jù)

#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;       //拉取數(shù)據(jù)
@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:@"拉取數(shù)據(jù)" 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{  
  //陀螺儀拉取數(shù)據(jù)
  CMGyroData *gyroData = [self.motionManager gyroData];
  CMRotationRate rotationrate = gyroData.rotationRate;
  NSLog(@"x = %.04f", rotationRate.x);
  NSLog(@"y = %.04f", rotationRate.y);
  NSLog(@"z = %.04f", rotationRate.z);
  
  //加速度計拉取數(shù)據(jù)
  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


Device Motion 拓展功能

 

CMMotionManager *manager = [[CMMotionManager alloc] init];
  
if ([manager isDeviceMotionAvailable] && ![manager isDeviceMotionActive]){
 
  manager.deviceMotionUpdateInterval = 0.01f;
  [manager startDeviceMotionUpdatesToQueue:[NSOperationQueue mainQueue]
               withHandler:^(CMDeviceMotion *data, NSError *error) {
                 double rotation = atan2(data.gravity.x, data.gravity.y) - M_PI;
                 self.imageView.transform = CGAffineTransformMakeRotation(rotation);
               }];
}

加速度計拓展功能

 

CMMotionManager *manager = [[CMMotionManager alloc] init];
manager.accelerometerUpdateInterval = 0.1;
  
if ([manager isAccelerometerAvailable] && ![manager isAccelerometerActive]){
    
  NSOperationQueue *queue = [[NSOperationQueue alloc] init];
  [manager startAccelerometerUpdatesToQueue:queue
                 withHandler:^(CMAccelerometerData *accelerometerData, NSError *error)
   {
     CMAcceleration acceleration = accelerometerData.acceleration;
       
     if (acceleration.x < -2.0) {
       dispatch_async(dispatch_get_main_queue(), ^{
         [self.navigationController popViewControllerAnimated:YES];
       });
     }
   }];
}

上述代碼, Demo地址

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

最新評論