国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

iOS CoreMotion實現設備運動加速度計陀螺儀

2020-07-26 02:34:56
字體:
來源:轉載
供稿:網友

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

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

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

設備運動

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

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

加速計

 

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

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

陀螺儀

 

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

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

代碼示例

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


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地址

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 崇州市| 临湘市| 紫云| 金溪县| 金昌市| 康乐县| 海南省| 丰城市| 青海省| 盐源县| 临沧市| 安义县| 丽水市| 英山县| 鱼台县| 隆昌县| 宁化县| 房山区| 昌黎县| 榆林市| 延寿县| 蒙山县| 邮箱| 盐城市| 霍林郭勒市| 兰州市| 陇川县| 达孜县| 长寿区| 涟源市| 阳谷县| 炎陵县| 福泉市| 延安市| 泾源县| 平昌县| 博野县| 承德县| 容城县| 赞皇县| 调兵山市|