陀螺仪简单使用

/
//  ViewController.m
//  陀螺仪使用
//
//  Created by ** on 16/7/28.
//  Copyright © 2016年 **. All rights reserved.
//

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


/*
    提前:使用陀螺仪需要导入CoreMotion框架
 */
@interface ViewController ()

/** 动作管理者 */
@property (nonatomic,strong)CMMotionManager *motionManager;
/** 队列 */
@property (nonatomic,strong)NSOperationQueue *quene;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    // 初始化
    self.motionManager = [[CMMotionManager alloc] init];
    self.quene = [[NSOperationQueue alloc] init];

    // 调用加速器
//    [self configureAccelerometer];

    // 调用陀螺仪
    [self configureGrro];
}

// 加速器
- (void)configureAccelerometer
{
    // 判断加速器是否可用,pull方式,5.0以后已经舍弃
//    if([_motionManager isAccelerometerAvailable]){
//        [_motionManager setAccelerometerUpdateInterval:1/40.0]; //设置加速器采样频率
//        [_motionManager startAccelerometerUpdates];
//        
//    }else{
//        NSLog(@"加速器不能使用");
//    }

    // push方式
    if([_motionManager isAccelerometerAvailable]){
        // 设置采样频率
        [_motionManager setAccelerometerUpdateInterval:1/40.0];
        // 开始采集数据
        [_motionManager startAccelerometerUpdatesToQueue:_quene withHandler:^(CMAccelerometerData * _Nullable accelerometerData, NSError * _Nullable error) {

            NSLog(@"%lf %lf %lf",accelerometerData.acceleration.x,accelerometerData.acceleration.y,accelerometerData.acceleration.z);

            if(fabs(accelerometerData.acceleration.x)>2.0||fabs(accelerometerData.acceleration.y) > 2.0||fabs(accelerometerData.acceleration.z) > 2.0){
                NSLog(@"检查到震荡");
            }

        }];
    }else
    {
        NSLog(@"加速器不能使用");
    }
}
// 陀螺仪使用
- (void)configureGrro
{
    if([_motionManager isGyroAvailable]){
        [_motionManager setGyroUpdateInterval:1];
        [self.motionManager startGyroUpdatesToQueue:_quene withHandler:^(CMGyroData * _Nullable gyroData, NSError * _Nullable error) {
            NSLog(@"%lf %lf %lf",gyroData.rotationRate.x,gyroData.rotationRate.y,gyroData.rotationRate.z);
        }];
    }else {
        NSLog(@"陀螺仪不能使用");
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值