首页 > 其他 > 详细

传感器- 加速计 - CoreMotion

时间:2015-09-24 12:41:21      阅读:229      评论:0      收藏:0      [点我收藏+]

/**

 *  CoreMotion

 *

 */

 

#import "ViewController.h"

#import <CoreMotion/CoreMotion.h> // 导入框架

 

 

@interface ViewController ()

@property (nonatomic, strong) CMMotionManager *mgr;// 必须搞成全局的

@end

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

   

//    [self push];

    

    [self pull];

}

 

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

    CMAcceleration acceleration = self.mgr.accelerometerData.acceleration;

    

    LogRed(@"%f --- %f  ---- %f",acceleration.x, acceleration.y, acceleration.z);

}

 

 

/**

 *  pull  --- 需要的时候, 采集

 */

- (void)pull

{

    // 1. 创建运动管理者对象

    self.mgr = [[CMMotionManager alloc] init];

    

    // 2. 判断加速计是否可用

    if (self.mgr.isAccelerometerAvailable) {

        

        // 4. 开始采样  ---  pull

        [self.mgr startAccelerometerUpdates];

    }else{

        LogGreen(@"加速计不可用");

    }

 

}

 

 

/**

 *  push --- 根据设置的采集时间间隔, 实时采集

 */

- (void)push

{

    // 1. 创建运动管理者对象

    self.mgr = [[CMMotionManager alloc] init];

    

    // 2. 判断加速计是否可用

    if (self.mgr.isAccelerometerAvailable) {

        /**

         *  accelerometerUpdateInterval --- 采样时间

         isAccelerometerActive       --- 是否正在采集

         startAccelerometerUpdates   --- pull

         startAccelerometerUpdatesToQueue  --- push

         stopAccelerometerUpdates    --- 停止采样

         accelerometerData           --- 采集到的数据

         */

        

        // 3. 设置采样间隔

        self.mgr.accelerometerUpdateInterval = 1.0 / 30.0;

        

        // 4. 开始采样

        [self.mgr startAccelerometerUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) {

            // 采集到数据时, 就会调用

            if(error) return;

            

            CMAcceleration acceleration = accelerometerData.acceleration;

            LogRed(@"%f --- %f  ---- %f",acceleration.x, acceleration.y, acceleration.z);

        }];

      

    }else{

        LogGreen(@"加速计不可用");

    }

}

传感器- 加速计 - CoreMotion

原文:http://www.cnblogs.com/guangleijia/p/4834836.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!