CoreMotion框架(三)—— 获取陀螺仪数据

版本记录

版本号 时间
V1.0 2017.08.05

前言

  我们的app很多都需要获取使用者的动作、方向以及其他和方位或者位置有关的参数,在ios中对应的框架就是CoreMotion,而在硬件对应的就是集成的加速计和陀螺仪。这几篇我们就从基础原理理论出发,讲一下相关的知识。关于这个框架的了解感兴趣的可以看这几篇。
1. CoreMotion框架(一)—— 基础理论
2. CoreMotion框架(二)—— 利用加速度计获取设备的方向

功能要求

利用陀螺仪获取设备的方向。


功能实现

  同利用加速度计获取设备的方向一样,利用陀螺仪获取数据同样也存在pullpush两种情况。这两种方式的异同点我就不说了,具体可以参考我前面写的那篇加速度计获取设备的方向。

push

下面我们就看一下push方式的代码。

1. JJGyroPushVC.m
#import "JJGyroPushVC.h"
#import 

@interface JJGyroPushVC ()

@property (nonatomic, strong) CMMotionManager *manager;


@end

@implementation JJGyroPushVC

#pragma mark - Override Base Function

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor greenColor];
    
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    self.manager = [[CMMotionManager alloc] init];
    self.manager.gyroUpdateInterval = 1.0/30;
    if ([self.manager isGyroAvailable]) {
        if (![self.manager isGyroActive]) {
            [self.manager startGyroUpdatesToQueue:queue withHandler:^(CMGyroData * _Nullable gyroData, NSError * _Nullable error) {
                
                //每秒变化的弧度,符合右手定则
                CGFloat x = gyroData.rotationRate.x;
                CGFloat y = gyroData.rotationRate.y;
                CGFloat z = gyroData.rotationRate.z;
                
                NSLog(@"x = %lf, y = %lf, z = %lf",x, y, z);
            }];
        }
    }
}

@end

下面我们看一下输出结果。

2017-08-05 22:20:14.368629+0800 JJOC[7836:3048233] x = 0.010673, y = -0.021204, z = 0.001318
2017-08-05 22:20:14.399169+0800 JJOC[7836:3048129] x = 0.010674, y = -0.022264, z = 0.001327
2017-08-05 22:20:14.428550+0800 JJOC[7836:3048233] x = 0.012812, y = -0.022258, z = 0.000276
2017-08-05 22:20:14.488608+0800 JJOC[7836:3048129] x = 0.008549, y = -0.022260, z = 0.000249
2017-08-05 22:20:14.518637+0800 JJOC[7836:3048233] x = 0.011745, y = -0.021198, z = 0.000260
2017-08-05 22:20:14.578544+0800 JJOC[7836:3048129] x = 0.009609, y = -0.022265, z = 0.001321
2017-08-05 22:20:14.608607+0800 JJOC[7836:3048233] x = 0.010673, y = -0.021204, z = 0.001318
2017-08-05 22:20:14.638713+0800 JJOC[7836:3048129] x = 0.010680, y = -0.022259, z = 0.000262
2017-08-05 22:20:14.698805+0800 JJOC[7836:3048233] x = 0.013879, y = -0.023318, z = 0.000292
2017-08-05 22:20:14.728615+0800 JJOC[7836:3048129] x = 0.009615, y = -0.022259, z = 0.000256
2017-08-05 22:20:14.788792+0800 JJOC[7836:3048233] x = 0.010680, y = -0.022259, z = 0.000262
2017-08-05 22:20:14.818813+0800 JJOC[7836:3048129] x = 0.009613, y = -0.021199, z = 0.000246
2017-08-05 22:20:14.878883+0800 JJOC[7836:3048233] x = 0.010674, y = -0.022264, z = 0.001327
2017-08-05 22:20:14.908880+0800 JJOC[7836:3048129] x = 0.009615, y = -0.022259, z = 0.000256
2017-08-05 22:20:14.938906+0800 JJOC[7836:3048233] x = 0.012814, y = -0.024378, z = 0.000294
2017-08-05 22:20:14.998943+0800 JJOC[7836:3048129] x = 0.005342, y = -0.019086, z = 0.001266
2017-08-05 22:20:15.029091+0800 JJOC[7836:3048233] x = 0.013873, y = -0.023323, z = 0.001357
2017-08-05 22:20:15.089012+0800 JJOC[7836:3048129] x = 0.010676, y = -0.023324, z = 0.001337
2017-08-05 22:20:15.118899+0800 JJOC[7836:3048233] x = 0.011748, y = -0.024379, z = 0.000288

pull

下面我们就看一下pull的方式。

直接看代码吧。

1. JJGyroPullVC.m
#import "JJGyroPullVC.h"
#import 

@interface JJGyroPullVC ()

@property (nonatomic, assign) CMRotationRate rotationRate;
@property (nonatomic, strong) CMMotionManager *manager;


@end

@implementation JJGyroPullVC

#pragma mark - Override Base Function

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor greenColor];
    
    self.manager = [[CMMotionManager alloc] init];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    if ([self.manager isGyroAvailable]) {
            [self.manager startGyroUpdates];
            self.rotationRate = self.manager.gyroData.rotationRate;
            [self startUpdate];
    }
}

#pragma mark - Object Private Function

- (void)startUpdate
{
    //每秒变化的弧度,符合右手定则
    CGFloat x = self.rotationRate.x;
    CGFloat y = self.rotationRate.y;
    CGFloat z = self.rotationRate.z;
    
    NSLog(@"x = %lf, y = %lf, z = %lf",x, y, z);
}

@end

下面看输出结果

2017-08-05 23:15:26.294232+0800 JJOC[7846:3054718] x = 0.226450, y = -0.247746, z = -0.042202
2017-08-05 23:15:26.925388+0800 JJOC[7846:3054718] x = 0.275163, y = -0.208465, z = 0.588230
2017-08-05 23:15:27.646075+0800 JJOC[7846:3054718] x = 1.572780, y = -0.181166, z = -0.281448
2017-08-05 23:15:27.927991+0800 JJOC[7846:3054718] x = 0.182105, y = -0.206188, z = -0.513569
2017-08-05 23:15:28.109596+0800 JJOC[7846:3054718] x = 0.428629, y = -0.804868, z = 0.511360
2017-08-05 23:15:28.361899+0800 JJOC[7846:3054718] x = 0.593156, y = -0.215569, z = 0.134465
2017-08-05 23:15:28.627982+0800 JJOC[7846:3054718] x = 0.176530, y = -0.114000, z = -0.292897
2017-08-05 23:15:28.877916+0800 JJOC[7846:3054718] x = -0.080725, y = 0.066879, z = -0.450517

后记

未完,待续~~~

CoreMotion框架(三)—— 获取陀螺仪数据_第1张图片
冰雪

你可能感兴趣的:(CoreMotion框架(三)—— 获取陀螺仪数据)