加速计的作用

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

加速计的经典应用场景

摇一摇

计步器

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

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. Windows Azure云服务价格调整通知

     好消息!由世纪互联运营的 Windows Azure推出优惠啦.我们采纳了多渠道客户的意见和建议,为了更好地服务大家,将降低多种云服务的价格,其中包括我们最受欢迎的服务 -虚拟机和 Block ...

  2. python for selenium 数据驱动测试

    # -*- coding:utf-8 -*- """ 数据驱动测试,从 csv 文件中读取数据 """ from selenium impo ...

  3. Xcode5最初级的教程

    相信IT男们,总会有那么一天希望自己捣鼓一个小App 让女朋友开心一下.那么就有了本文的开始的动机,话说带着兴趣做事情的时候进度是最快的也是最轻松的,这也是因为为什么有女朋友陪着的时候走多远的路脚都不 ...

  4. [Linux&Vim]基础01

    学习和使用Linux有一段时间了,作为这么长时间的学习应该是有一定收获的,可惜的是没有一股专研精神,只停留在皮毛. 阅读过一些大牛们学习Linux和Vim的过程,从命令.环境配置.使用个性化设置.编程 ...

  5. opengl performance optimization

    OpenGL 性能优化 作者: Yang Jian (jyang@cad.zju.edu.cn) 日期: 2009-05-04 本文从硬件体系结构.状态机.光照.纹理.顶点数组.LOD.Cull等方面 ...

  6. GDI+ —— Tcanvas 类属性及方法.......

    delphi TCanvas类 类关系   TObject-> TPersistent   对那些作图对象,可使用TCanvas对象作为画布.标准的window控件,例如编辑控件和列表框控件,当 ...

  7. [LeetCode] Word Break 解题思路

    Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...

  8. zabbix 编译

    yum -y install xml* libxml* net-snmp net-snmp* php-bcmath ./configure --enable-server --enable-agent ...

  9. 【Android - MD】之FloatingActionButton的使用

    FloatingActionButton(FAB) 是 Android 5.0 新特性--Material Design 中的一个控件,是一种悬浮的按钮. FloatingActionButton 是 ...

  10. Qt 学习之路:线程总结

    前面我们已经详细介绍过有关线程的一些值得注意的事项.现在我们开始对线程做一些总结. 有关线程,你可以做的是: 在QThread子类添加信号.这是绝对安全的,并且也是正确的(前面我们已经详细介绍过,发送 ...