加速计的作用

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

加速计的经典应用场景

摇一摇

计步器

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

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. lc面试准备:Power of Two

    1 题目 Given an integer, write a function to determine if it is a power of two. 接口 boolean isPowerOfTw ...

  2. bzoj2466,poj1222

    都是简单的异或高斯消元 由于bzoj2466要求解得最小和,所以我们最后还要穷举自由元取最优解 type node=record        po,next:longint;      end; . ...

  3. MySQL的Grant命令

    来源:http://yingxiong.javaeye.com/blog/451208   本文实例,运行于 MySQL 5.0 及以上版本. MySQL 赋予用户权限命令的简单格式可概括为: gra ...

  4. java Spring使用配置文件读取jdbc.properties

    Spring使用配置文件读取jdbc.properties 在beans.xml中加入两个必须的bean [html]<bean id="propertyConfigurer" ...

  5. winphone 开发学习笔记(1)

    学习了winphone task 1.首先根据自己电脑下载sdk,,,win 7和win 8的Sdk安装包不一样, http://www.microsoft.com/en-hk/download/de ...

  6. Python copy对象(copy与deepcopy)

    Python中的对象之间赋值时是按引用传递的,如果需要拷贝对象,需要使用标准库中的copy模块. 1. copy.copy 浅拷贝 只拷贝父对象,不会拷贝对象的内部的子对象. 2. copy.deep ...

  7. 解压华为P6 UPDATE.APP

    #!/usr/bin/env python # Version: 0.2.201308040830 # Author: linkscue # Function: unpack any hauwei h ...

  8. jump_ur.php通知模板

    <title>出错啦~~~</title> <script language="javascript" type="text/javascr ...

  9. 来看看Github上流行的编码规范

      Popular Coding Convention on Github是一个有趣的网站,它根据Github的代码提交情况分析了几种语言流行的代码规范,目前支持对JavaScript,Java,Py ...

  10. 通过 INotifyPropertyChanged 实现观察者模式

    INotifyPropertyChanged 它的作用:向客户端发出某一属性值已更改的通知. 当属性改变时,它可以通知客户端,并进行界面数据更新.而我们不用写很多复杂的代码来更新界面数据,这样可以做到 ...