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ù)提供了一種簡(jiǎn)單的方法,讓您獲取應(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)前的磁場(chǎng)矢量
加速計(jì)
該圖為,加速度計(jì)沿x,y和z軸的速度變化
加速度計(jì)測(cè)量沿一個(gè)軸的速度變化。 所有的iOS設(shè)備都有一個(gè)三軸加速度計(jì),它在圖1所示的三個(gè)軸中的每一個(gè)軸上提供加速度值。加速度計(jì)報(bào)告的值以重力加速度的增量進(jìn)行測(cè)量,值1.0代表9.8米的加速度 每秒(每秒)在給定的方向。 取決于加速度的方向,加速度值可能是正值或負(fù)值。
陀螺儀
該圖為,旋轉(zhuǎn)反向速率對(duì)陀螺儀繞x,y和z軸的影響變化
陀螺儀測(cè)量設(shè)備圍繞空間軸旋轉(zhuǎn)的速率。 許多iOS設(shè)備都有一個(gè)三軸陀螺儀,它可以在圖1所示的三個(gè)軸中的每一個(gè)軸上提供旋轉(zhuǎn)值。旋轉(zhuǎn)值以給定軸每秒的弧度為單位進(jìn)行測(cè)量。 根據(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地址
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
IOS App圖標(biāo)和啟動(dòng)畫面尺寸詳細(xì)介紹
這篇文章主要介紹了IOS App圖標(biāo)和啟動(dòng)畫面尺寸詳細(xì)介紹的相關(guān)資料,需要的朋友可以參考下2017-02-02
詳解iOS開發(fā) - 用AFNetworking實(shí)現(xiàn)https單向驗(yàn)證,雙向驗(yàn)證
這篇文章主要介紹了詳解iOS開發(fā) - 用AFNetworking實(shí)現(xiàn)https單向驗(yàn)證,雙向驗(yàn)證,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2016-12-12
iOS實(shí)現(xiàn)類似微信和支付寶的密碼輸入框(UIKeyInput協(xié)議)
這篇文章主要介紹了iOS實(shí)現(xiàn)類似微信和支付寶的密碼輸入框,通過UIKeyInput協(xié)議為響應(yīng)者提供簡(jiǎn)單的鍵盤輸入的功,再通過CoreGraphics繪制出密碼輸入框,感興趣的小伙伴們可以參考一下2016-08-08
iOS仿支付寶芝麻信用分?jǐn)?shù)儀表盤動(dòng)畫效果
這篇文章主要為大家詳細(xì)介紹了iOS仿支付寶芝麻信用分?jǐn)?shù)儀表盤動(dòng)畫效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09
iOS中管理剪切板的UIPasteboard粘貼板類用法詳解
在iOS中,通過UITextField、UITextView和UIWebView剪切或復(fù)制的內(nèi)容都可以通過UIPasteboard類來管理粘貼操作,下面就為大家?guī)韎OS中管理剪切板的UIPasteboard粘貼板類用法詳解:2016-06-06
iOS開發(fā)中實(shí)現(xiàn)顯示gif圖片的方法
這篇文章主要介紹了iOS開發(fā)中實(shí)現(xiàn)顯示gif圖片的方法,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2015-09-09
iOS 獲取當(dāng)前的ViewController的方法
本篇文章主要介紹了iOS 獲取當(dāng)前的ViewController的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-09-09
iOS微信瀏覽器回退不刷新實(shí)例(監(jiān)聽瀏覽器回退事件)
下面小編就為大家?guī)硪黄猧OS微信瀏覽器回退不刷新實(shí)例(監(jiān)聽瀏覽器回退事件)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-05-05

