陀螺仪CoreMotion - (Obj-C)

与加速计一样,需要先导入<CoreMotion/CoreMotion.h>头文件,然后创建一个管理者

CoreMotion中获取传感器数据有两种方式
1.Push : 系统主动推送给客户端 实时性强,能耗大
2.Pull : 客户端主要向系统去获取数据 实时性差,能耗小,按需获取

通过是否设置更新间隔来区分,一旦设置了更新间隔,表示使用Push方式,如果使用Pull方式,按需获取,通过管理者的gyroData属性直接得到数据

Push方式:

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

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

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 1. 创建运动管理者
    self.manager = [[CMMotionManager alloc]init];
    
    // 2. 设置间隔时间
    self.manager.gyroUpdateInterval = 1.0f;
    
    // 3. 开启监测
    [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. 创建运动管理者
    self.manager = [[CMMotionManager alloc]init];
    
    // 2. 开启监测
    [self.manager startGyroUpdates];
}

// 点击屏幕时获取监测数据
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    // 3. 获取监测数据
    CMGyroData *gyroData = self.manager.gyroData;
    CMRotationRate rotationRate = gyroData.rotationRate;
    NSLog(@"%f,%f,%f",rotationRate.x,rotationRate.y,rotationRate.z);
    
}

@end

打印结果:

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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,273评论 19 139
  • UIAccelerometer在iOS 5中已经过期,iOS 4以后使用CoreMotion.framework不...
    ShenYj阅读 3,008评论 0 1
  • 磁力计用来检测地球磁场,与加速计、陀螺仪一样,需要先导入<CoreMotion/CoreMotion.h>头文件,...
    ShenYj阅读 3,060评论 0 1
  • 消息队列设计精要 消息队列已经逐渐成为企业IT系统内部通信的核心手段。它具有低耦合、可靠投递、广播、流量控制、最终...
    meng_philip123阅读 5,399评论 1 25
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 177,185评论 25 709

友情链接更多精彩内容