加速计的作用

用于检测设备的运动(比如摇晃)

加速计的经典应用场景

摇一摇

计步器

**********************************

Core Motion获取数据的两种方式

push

实时采集所有数据(采集频率高)

pull

在有需要的时候,再主动去采集数据

 #import "ViewController.h"
#import <CoreMotion/CoreMotion.h> @interface ViewController () @property (strong, nonatomic) CMMotionManager *mgr; @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; /* pull方法 */ // 创建加速计管理对象
self.mgr = [[CMMotionManager alloc] init]; // 判断加速计是否可用
if (self.mgr.isAccelerometerAvailable) {
NSLog(@"加速计可用");
// 开始采集(pull) - 这样之后就只会在需要的时候才采集数据, 并放到管理对象的accelerometerData中
[self.mgr startAccelerometerUpdates]; } else {
NSLog(@"加速计不可用");
}
} - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CMAcceleration acceleration = self.mgr.accelerometerData.acceleration;
NSLog(@"x = %f, y = %f, z = %f", acceleration.x, acceleration.y, acceleration.z);
} /**
* push方式
*/
- (void)push
{
// 创建加速计管理对象
self.mgr = [[CMMotionManager alloc] init]; // 判断加速计是否可用
if (self.mgr.isAccelerometerAvailable) {
NSLog(@"加速计可用");
// 设置采集时间间隔
self.mgr.magnetometerUpdateInterval = / 30.0;
// 开始采集(push)
[self.mgr startAccelerometerUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) {
if (error) return;
// 采集到数据就会调用这个方法
NSLog(@"x = %f, y = %f, z = %f", accelerometerData.acceleration.x, accelerometerData.acceleration.y, accelerometerData.acceleration.z);
}]; } else {
NSLog(@"加速计不可用");
}
}

CoreMotion(加速计)的更多相关文章

  1. iFIERO - (二)宇宙大战 Space Battle -- SpriteKit 无限循环背景Endless、SpriteKit物理碰撞、CoreMotion加速计

    本节主要讲解如何创建无限循环Endless的星空背景(如下图).玩家飞船发射子弹,监测子弹击外星敌机的SpriteKit物理碰撞并消灭敌机,以及应用iOS的CoreMotion加速计移动飞船躲避外星敌 ...

  2. 加速计 & CoreMotion

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

  3. ios开发——实用技术篇Swift篇&加速计和陀螺仪

    加速计和陀螺仪 //返回按钮事件 @IBAction func backButtonClick() { self.navigationController?.popViewControllerAnim ...

  4. (三)宇宙大战 Space Battle -- 场景SCENE切换、UserDefaults统计分数、Particle粒子效果

    此<宇宙大战 Space Battle>SpirteKit手机游戏教程共分为三系列: (一)宇宙大战 Space Battle -- 新建场景Scene.精灵节点.Particle粒子及背 ...

  5. 传感器- 加速计 - CoreMotion

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

  6. iOS开发-CoreMotion框架(加速计和陀螺仪)

    CoreMotion是一个专门处理Motion的框架,其中包含了两个部分加速度计和陀螺仪,在iOS4之前加速度计是由UIAccelerometer类来负责采集数据,现在一般都是用CoreMotion来 ...

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

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

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

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

  9. iOS新加速计事件(陀螺仪和加速计)

    iOS新加速计事件 [iOS新加速计事件] 1.iOS5.0以前,可以使用UIAcceleration来监听加速计事件. 2.Bug iOS5.0以后,UIAccelerometerDelegate已 ...

随机推荐

  1. Host group 信息

  2. openwrt使用3G上网卡

    尊敬的大大.感谢你抽空指导我 我的设备是db120 mu350 和广东无限卡 版本是OpenWrt Backfire 10.03.336 DIY full 一.        没有安装到kmod-us ...

  3. 在多台服务器上简单实现Redis的数据主从复制

          Redis的主从复制功能非常强大,一个master可以拥有多个slave,而一个slave又可以拥有多个slave,如此下去,形成了强大的多级服务器集群架构.下面我演示下怎样在多台服务器上 ...

  4. suse linux 编译安装Apache时报“APR NOT FOUND”的解决方法

    今日编译apache时出错: #./configure --prefix……检查编辑环境时出现: checking for APR... noconfigure: error: APR not fou ...

  5. STM32F072B-DISCO 深入研究 中断系统

    STM32F072B-DISCO 是我认为性价比最高的一款CPU的demo系统,以前一直在用PIC的CPU但最近几年ST异军突起,几次课题查找芯片无一例外都是ST,像USB,CAN,ZIGBEE等,S ...

  6. ACM2096_小明A+B

    #include<iostream> int main() { using namespace std; int a,b,count; cin>>count; while(co ...

  7. 【转载】Manacher算法

    本文原创:http://www.cnblogs.com/BigBallon/p/3816890.html只为了记录学习,不为抄袭!http://www.felix021.com/blog/read.p ...

  8. 2 storm的topology提交执行

    本博文的主要内容有 .storm单机模式,打包,放到storm集群 .Storm的并发机制图 .Storm的相关概念 .附PPT 打包,放到storm集群去.我这里,是单机模式下的storm. wee ...

  9. 【长篇高能】ReactiveCocoa 和 MVVM 入门

    翻译自ReactiveCocoa and MVVM, an Introduction. 文中引用的 Gist 可能无法显示.为了和谐社会, 请科学上网. MVC 任何一个正经开发过一阵子软件的人都熟悉 ...

  10. 由链表初始化看C语言的二级指针

    先来看C语言创建链表.插入节点和遍历链表的一段代码: #include <stdio.h> #include <stdlib.h> typedef int ElemType; ...