iOS CoreMotion實(shí)現(xiàn)設(shè)備運(yùn)動(dòng)加速度計(jì)陀螺儀
用于處理加速度計(jì),陀螺儀,計(jì)步器和與環(huán)境有關(guān)的事件。
Core Motion框架從iOS設(shè)備的板載硬件(包括加速計(jì),陀螺儀,計(jì)步器,磁力計(jì)和氣壓計(jì))報(bào)告與運(yùn)動(dòng)和環(huán)境有關(guān)的數(shù)據(jù)。您可以使用此框架訪問硬件生成的數(shù)據(jù),以便您可以在應(yīng)用程序中使用它。例如,游戲可能使用加速度計(jì)和陀螺儀數(shù)據(jù)來控制屏幕上的游戲行為。 這個(gè)框架的許多服務(wù)都可以訪問硬件記錄的原始值和這些值的處理版本。處理后的值不包括第三方因素對(duì)該數(shù)據(jù)的造成不利影響的情況。例如,處理的加速度計(jì)值僅反映由用戶引起的加速度,而不是由重力引起的加速度。
在iOS 10.0或之后版本的iOS應(yīng)用程序必須在其Info.plist文件中包含使用說明Key的描述,以告知用戶獲取所需的數(shù)據(jù)類型及獲取數(shù)據(jù)類型的目的。未能包含這些Key會(huì)導(dǎo)致應(yīng)用程序崩潰。特別是訪問運(yùn)動(dòng)和健身數(shù)據(jù)時(shí),必須聲明 NSMotionUsageDescription
設(shè)備運(yùn)動(dòng)
設(shè)備運(yùn)動(dòng)服務(wù)提供了一種簡單的方法,讓您獲取應(yīng)用程序的運(yùn)動(dòng)相關(guān)數(shù)據(jù)。原始的加速度計(jì)和陀螺儀數(shù)據(jù)需要處理,以消除其他因素(如重力)的偏差。設(shè)備運(yùn)動(dòng)服務(wù)為您處理這些數(shù)據(jù),為您提供可以立即使用的精確數(shù)據(jù)。例如,此服務(wù)為用戶啟動(dòng)的加速度和重力引起的加速度提供單獨(dú)的值。因此,此服務(wù)可讓您專注于使用數(shù)據(jù)來操縱您的內(nèi)容,而不是處理該數(shù)據(jù)。 設(shè)備運(yùn)動(dòng)服務(wù)使用可用的硬件來生成CMDeviceMotion對(duì)象,其中包含以下信息:
- 設(shè)備在三維空間中相對(duì)于參考框架的方向(或姿態(tài))
- 無偏的旋轉(zhuǎn)速度
- 當(dāng)前重力矢量
- 用戶生成的加速度矢量(無重力)
- 當(dāng)前的磁場矢量
加速計(jì)
該圖為,加速度計(jì)沿x,y和z軸的速度變化
加速度計(jì)測量沿一個(gè)軸的速度變化。 所有的iOS設(shè)備都有一個(gè)三軸加速度計(jì),它在圖1所示的三個(gè)軸中的每一個(gè)軸上提供加速度值。加速度計(jì)報(bào)告的值以重力加速度的增量進(jìn)行測量,值1.0代表9.8米的加速度 每秒(每秒)在給定的方向。 取決于加速度的方向,加速度值可能是正值或負(fù)值。
陀螺儀
該圖為,旋轉(zhuǎn)反向速率對(duì)陀螺儀繞x,y和z軸的影響變化
陀螺儀測量設(shè)備圍繞空間軸旋轉(zhuǎn)的速率。 許多iOS設(shè)備都有一個(gè)三軸陀螺儀,它可以在圖1所示的三個(gè)軸中的每一個(gè)軸上提供旋轉(zhuǎn)值。旋轉(zhuǎn)值以給定軸每秒的弧度為單位進(jìn)行測量。 根據(jù)旋轉(zhuǎn)的方向,旋轉(zhuǎn)值可以是正值或負(fù)值。
代碼示例
Push方式獲取數(shù)據(jù)
加速度計(jì)
CMMotionManager *manager = [[CMMotionManager alloc] init]; if ([manager isAccelerometerAvailable] && ![manager isAccelerometerActive]){ NSOperationQueue *queue = [[NSOperationQueue alloc] init]; manager.accelerometerUpdateInterval = 0.1;//設(shè)置信息更新頻率(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;//設(shè)置信息更新頻率(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; //啟動(dòng) 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:@"啟動(dòng)" 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); //加速度計(jì)拉取數(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]; } //加速度計(jì) 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); }]; }
加速度計(jì)拓展功能
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)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
iOS 獲取公歷、農(nóng)歷日期的年月日的實(shí)例代碼
本篇文章主要介紹了iOS 獲取公歷、農(nóng)歷日期的年月日的實(shí)例代碼,主要介紹了三種方法,具有一定的參考價(jià)值,有興趣的可以了解一下。2017-02-02iOS使用Masonry時(shí)進(jìn)行動(dòng)畫操作方法
下面小編就為大家分享一篇iOS使用Masonry時(shí)進(jìn)行動(dòng)畫操作方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-03-03iOS tableView實(shí)現(xiàn)下拉圖片放大效果
這篇文章主要為大家詳細(xì)介紹了iOS tableView實(shí)現(xiàn)下拉圖片放大效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05簡單好用的iOS導(dǎo)航欄封裝.runtime屬性控制實(shí)例代碼
這篇文章主要給大家介紹了簡單好用的iOS導(dǎo)航欄封裝.runtime屬性控制的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-10-10iOS開發(fā)之詳談屬性設(shè)置readwrite、readonly、retain、copy、assign、nonatomic
這篇文章主要介紹了iOS開發(fā)之詳談屬性設(shè)置readwrite、readonly、retain、copy、assign、nonatomic的相關(guān)資料,需要的朋友可以參考下2015-10-10iOS應(yīng)用中UILabel文字顯示效果的常用設(shè)置總結(jié)
UILabel組件可以用來設(shè)置文字內(nèi)容的排版與字體效果等,功能非常多,下面就來為大家整理一下基本的iOS應(yīng)用中UILabel文字顯示效果的常用設(shè)置總結(jié)2016-05-05