陀螺儀CoreMotion - (Obj-C)

與加速計一樣,需要先導(dǎo)入<CoreMotion/CoreMotion.h>頭文件,然后創(chuàng)建一個管理者

CoreMotion中獲取傳感器數(shù)據(jù)有兩種方式
1.Push : 系統(tǒng)主動推送給客戶端 實時性強,能耗大
2.Pull : 客戶端主要向系統(tǒng)去獲取數(shù)據(jù) 實時性差,能耗小,按需獲取

通過是否設(shè)置更新間隔來區(qū)分,一旦設(shè)置了更新間隔,表示使用Push方式,如果使用Pull方式,按需獲取,通過管理者的gyroData屬性直接得到數(shù)據(jù)

Push方式:

#import "ViewController.h"
#import <CoreMotion/CoreMotion.h>

@interface ViewController ()
@property (nonatomic,strong) CMMotionManager *manager;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 1. 創(chuàng)建運動管理者
    self.manager = [[CMMotionManager alloc]init];
    
    // 2. 設(shè)置間隔時間
    self.manager.gyroUpdateInterval = 1.0f;
    
    // 3. 開啟監(jiān)測
    [self.manager startGyroUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMGyroData * _Nullable gyroData, NSError * _Nullable error) {
        
        CMRotationRate rotationRate = gyroData.rotationRate;
        NSLog(@"%f,%f,%f",rotationRate.x,rotationRate.y,rotationRate.z);
        
    }];
    
}

@end

Pull方式:

#import "ViewController.h"
#import <CoreMotion/CoreMotion.h>

@interface ViewController ()
@property (nonatomic,strong) CMMotionManager *manager;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 1. 創(chuàng)建運動管理者
    self.manager = [[CMMotionManager alloc]init];
    
    // 2. 開啟監(jiān)測
    [self.manager startGyroUpdates];
}

// 點擊屏幕時獲取監(jiān)測數(shù)據(jù)
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    // 3. 獲取監(jiān)測數(shù)據(jù)
    CMGyroData *gyroData = self.manager.gyroData;
    CMRotationRate rotationRate = gyroData.rotationRate;
    NSLog(@"%f,%f,%f",rotationRate.x,rotationRate.y,rotationRate.z);
    
}

@end

打印結(jié)果:

2016-07-14 18:54:42.893 03-陀螺儀[4338:4669017] -0.849916,2.210453,0.640329
2016-07-14 18:54:43.305 03-陀螺儀[4338:4669017] 4.547867,-4.541954,-1.075862
2016-07-14 18:54:43.621 03-陀螺儀[4338:4669017] -7.368330,10.033870,2.520494
2016-07-14 18:54:44.039 03-陀螺儀[4338:4669017] -1.995928,-0.730891,-0.609648
2016-07-14 18:54:44.256 03-陀螺儀[4338:4669017] 4.259613,-2.782650,-0.929494
2016-07-14 18:54:44.505 03-陀螺儀[4338:4669017] 2.903455,-3.432033,-0.955542
2016-07-14 18:54:44.722 03-陀螺儀[4338:4669017] -1.869434,1.511082,-0.031730
2016-07-14 18:54:44.888 03-陀螺儀[4338:4669017] -0.701699,0.812793,-0.336599
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,540評論 19 139
  • UIAccelerometer在iOS 5中已經(jīng)過期,iOS 4以后使用CoreMotion.framework不...
    ShenYj閱讀 438評論 0 1
  • 磁力計用來檢測地球磁場,與加速計、陀螺儀一樣,需要先導(dǎo)入<CoreMotion/CoreMotion.h>頭文件,...
    ShenYj閱讀 448評論 0 1
  • 消息隊列設(shè)計精要 消息隊列已經(jīng)逐漸成為企業(yè)IT系統(tǒng)內(nèi)部通信的核心手段。它具有低耦合、可靠投遞、廣播、流量控制、最終...
    meng_philip123閱讀 1,581評論 1 25
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,941評論 25 709

友情鏈接更多精彩內(nèi)容