/**

*  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的更多相关文章

  1. 加速计 & CoreMotion

    CHENYILONG Blog 加速计 & CoreMotion 加速计 & CoreMotion 技术博客http://www.cnblogs.com/ChenYilong/ 新浪微 ...

  2. Swift - 加速传感器(CoreMotion)的用法,小球加速运动并反弹样例

    1,加速传感器可以监听到x,y,z三个方向的加速度,使用步骤如下: (1)实例化CMMotionManager类 (2)向CMMotionManager的accelerometerUpdateInte ...

  3. iOS开发 传感器(加速计、摇一摇、计步器)

    一.传感器 1.什么是传感器传感器是一种感应\检测周围环境的一种装置, 目前已经广泛应用于智能手机上 传感器的作用用于感应\检测设备周边的信息不同类型的传感器, 检测的信息也不一样 iPhone中的下 ...

  4. iOS开发——高级篇——传感器(加速计、摇一摇、计步器)

    一.传感器 1.什么是传感器传感器是一种感应\检测周围环境的一种装置, 目前已经广泛应用于智能手机上 传感器的作用用于感应\检测设备周边的信息不同类型的传感器, 检测的信息也不一样 iPhone中的下 ...

  5. 推送通知/传感器/UIDynamic仿真(推送通知已适配iOS10)

    推送通知/传感器/UIDynamic 一.推送通知 1.推送通知简介 什么是推送通知 此处的推送通知与NSNotification没有任何关系 可以理解为,向用户推送一条信息来通知用户某件事情 作用: ...

  6. iOS 传感器集锦

    https://www.jianshu.com/p/5fc26af852b6 传感器集锦:指纹识别.运动传感器.加速计.环境光感.距离传感器.磁力计.陀螺仪   效果预览.gif 一.指纹识别 应用: ...

  7. iOS---开发实用传感器

    传感器 1.什么是传感器 传感器是一种感应\检测装置, 目前已经广泛应用于智能手机上 2.传感器的作用 用于感应\检测设备周边的信息 不同类型的传感器, 检测的信息也不一样 iPhone中的下面现象都 ...

  8. iphone传感器

    传感器 什么是传感器 传感器是一种感应\检测装置, 目前已经广泛应用于智能手机上 传感器的作用 用于感应\检测设备周边的信息 不同类型的传感器, 检测的信息也不一样 iPhone中的下面现象都是由传感 ...

  9. IOS UIDevice距离传感器(打开 关闭)

    ●  什么是传感器 ●  传感器是一种感应\检测装置, 目前已经广泛应用于智能手机上 ●  iPhone5中内置的传感器有 ●  运动传感器\加速度传感器\加速计(Motion/Acceleromet ...

随机推荐

  1. Monitoring and Tuning the Linux Networking Stack: Receiving Data

    http://blog.packagecloud.io/eng/2016/06/22/monitoring-tuning-linux-networking-stack-receiving-data/ ...

  2. Unicode编码及其实现:UTF-16、UTF-8,and more

    http://blog.csdn.net/thl789/article/details/7506133

  3. patchdiff2 函数比较插件

    https://code.google.com/archive/p/patchdiff2/downloads

  4. careercup-数学与概率 7.6

    7.6 在二维平面上,有一些点,请找出经过点数最多的那条线. 解法: 类似于leetcode:Max Points on a Line 我们只需在任意两点之间“画”一条无限长的直线(也即不是线段),并 ...

  5. Foundation: NSNotificationCenter

    一个NSNotificationCenter对象(通知中心)提供了在程序中广播消息的机制,它实质上就是一个通知分发表.这个分发表负责维护为各个通知注册的观察者,并在通知到达时,去查找相应的观察者,将通 ...

  6. Java基础知识强化之集合框架笔记73:如何选择使用哪种集合

    1. 到底使用那种集合.    看需求 是否是键值对象形式: 是:Map 键是否需要排序: 是:TreeMap 否:HashMap 不知道,就使用HashMap. 否:Collection 元素是否唯 ...

  7. 两种JSON数据类型的解析

    son数据格式解析我自己分为两种: 一种是普通的,一种是带有数组形式的: 普通形式的:服务器端返回的json数据格式如下: {"userbean":{"Uid" ...

  8. Android 自学之表格布局 TableLayout

    表格布局(TableLayout),表格布局采用行.列的形式来管理UI组件,TableLayout并不需要明确的声明多少行,多少列,而是通过TableRow.其他组件来控制表格的行数和列数. 每次想T ...

  9. Mysql权限对照表

    Mysql分为5种数据权限.12种结构权限.11种管理权限,当我们需要精细化权限的时候,我们就需要对照来进行授权,这样可以很好的的控制登录人员的权限.

  10. (转)Eclipse中junit框架的使用——单元测试

    [转]junit浅学笔记一 JUnit是一个回归测试框架(regression testing framework).Junit测试是程序员测试,即所谓白盒测试,因为程序员知道被测试的软件如何(How ...