CoreMotion(加速计)
加速计的作用
用于检测设备的运动(比如摇晃)
加速计的经典应用场景
摇一摇
计步器
**********************************
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(加速计)的更多相关文章
- iFIERO - (二)宇宙大战 Space Battle -- SpriteKit 无限循环背景Endless、SpriteKit物理碰撞、CoreMotion加速计
本节主要讲解如何创建无限循环Endless的星空背景(如下图).玩家飞船发射子弹,监测子弹击外星敌机的SpriteKit物理碰撞并消灭敌机,以及应用iOS的CoreMotion加速计移动飞船躲避外星敌 ...
- 加速计 & CoreMotion
CHENYILONG Blog 加速计 & CoreMotion 加速计 & CoreMotion 技术博客http://www.cnblogs.com/ChenYilong/ 新浪微 ...
- ios开发——实用技术篇Swift篇&加速计和陀螺仪
加速计和陀螺仪 //返回按钮事件 @IBAction func backButtonClick() { self.navigationController?.popViewControllerAnim ...
- (三)宇宙大战 Space Battle -- 场景SCENE切换、UserDefaults统计分数、Particle粒子效果
此<宇宙大战 Space Battle>SpirteKit手机游戏教程共分为三系列: (一)宇宙大战 Space Battle -- 新建场景Scene.精灵节点.Particle粒子及背 ...
- 传感器- 加速计 - CoreMotion
/** * CoreMotion * */ #import "ViewController.h" #import <CoreMotion/CoreMotion.h> ...
- iOS开发-CoreMotion框架(加速计和陀螺仪)
CoreMotion是一个专门处理Motion的框架,其中包含了两个部分加速度计和陀螺仪,在iOS4之前加速度计是由UIAccelerometer类来负责采集数据,现在一般都是用CoreMotion来 ...
- iOS开发 传感器(加速计、摇一摇、计步器)
一.传感器 1.什么是传感器传感器是一种感应\检测周围环境的一种装置, 目前已经广泛应用于智能手机上 传感器的作用用于感应\检测设备周边的信息不同类型的传感器, 检测的信息也不一样 iPhone中的下 ...
- iOS开发——高级篇——传感器(加速计、摇一摇、计步器)
一.传感器 1.什么是传感器传感器是一种感应\检测周围环境的一种装置, 目前已经广泛应用于智能手机上 传感器的作用用于感应\检测设备周边的信息不同类型的传感器, 检测的信息也不一样 iPhone中的下 ...
- iOS新加速计事件(陀螺仪和加速计)
iOS新加速计事件 [iOS新加速计事件] 1.iOS5.0以前,可以使用UIAcceleration来监听加速计事件. 2.Bug iOS5.0以后,UIAccelerometerDelegate已 ...
随机推荐
- 网络流CodeForces. Original 589F:Gourmet and Banquet
A gourmet came into the banquet hall, where the cooks suggested n dishes for guests. The gourmet kno ...
- 汇编学习笔记(11)int指令和端口
格式 int指令也是一种内中断指令,int指令的格式为int n,n是中断类型码.也就是说,使用int指令可以调用任意的中断例程,例如我们可以显示的调用0号中断例程,还记得在汇编学习笔记(10)中我们 ...
- leetcode Longest Common Prefix 多个字符串的最长字串
public class Solution { public String get(String a,String b) { if(a==""||b=="") ...
- bzoj 2463 [中山市选2009]谁能赢呢?(博弈)
2463: [中山市选2009]谁能赢呢? Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1290 Solved: 944[Submit][Stat ...
- UIColor的使用
UIColor主要在设置各个控件的颜色的时候用得到,主要有以下几种方法创建UIColor: 1.使用RGBA创建: greenbluealpha]; 2.直接创建各个常用颜色: [UIColo ...
- 【原】centos6.5下cdh4.6 Oozie安装
0.oozie只需安装在一台服务器上,这里选择在namenode上来安装:安装用户为cloud-user 1.安装Oozie包: sudo yum install -y oozie oozie- ...
- 以O2O为核心的ERP思考
O2O已经火了一阵子了,很多人都在说O2O,各行各业都想和O2O有所结合,都认为这里面将会有巨大的商机. 在互联网发展到移动互联网的时代,我们的生活的很多方面已经被改变了,很多事情都已经可以在移动端完 ...
- mongodb基本概念解析
MongoDB 概念解析 不管我们学习什么数据库都应该学习其中的基础概念,在mongodb中基本的概念是文档.集合.数据库,下面我们挨个介绍. 下表将帮助您更容易理解Mongo中的一些概念: SQL术 ...
- DevExpress 控件 GridControl常见用法
刚接触DevExpress第三方控件,把GridControl的常见用法整理一下,以供参考: 说明: gcTest GridControl gvText GridView //隐藏最上面的G ...
- HDU ACM 1025 Constructing Roads In JGShining's Kingdom->二分求解LIS+O(NlogN)
#include<iostream> using namespace std; //BFS+优先队列(打印路径) #define N 500005 int c[N]; int dp[N]; ...