iOS开发-CoreMotion框架(加速计和陀螺仪)
CoreMotion是一个专门处理Motion的框架,其中包含了两个部分加速度计和陀螺仪,在iOS4之前加速度计是由UIAccelerometer类来负责采集数据,现在一般都是用CoreMotion来处理加速度过程,不过由于UIAccelerometer比较简单,同样有人在使用。加速计由三个坐标轴决定,用户最常见的操作设备的动作移动,晃动手机(摇一摇),倾斜手机都可以被设备检测到,加速计可以检测到线性的变化,陀螺仪可以更好的检测到偏转的动作,可以根据用户的动作做出相应的动作,iOS模拟器无法模拟以上动作,真机调试需要开发者账号。
加速计
加速计的x,y,z三个方向,参考下图:
如果只需要知道设备的方向,不需要知道具体方向矢量角度,那么可以使用UIDevice进行操作,还可以根据方向就行判断,具体可以参考一下苹果官网代码:
-(void) viewDidLoad {
// Request to turn on accelerometer and begin receiving accelerometer events
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:nil];
} - (void)orientationChanged:(NSNotification *)notification {
// Respond to changes in device orientation
} -(void) viewDidDisappear {
// Request to stop receiving accelerometer events and turn off accelerometer
[[NSNotificationCenter defaultCenter] removeObserver:self];
[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
}
当用户晃动设备的时候,系统会通知每一个在用的设备,可以使本身成为第一响应者:
- (BOOL)canBecomeFirstResponder {
return YES;
} - (void)viewDidAppear:(BOOL)animated {
[self becomeFirstResponder];
}
处理Motion事件有三种方式,开始(motionBegan),结束(motionEnded),取消(motionCancelled):
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event NS_AVAILABLE_IOS(3_0);
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event NS_AVAILABLE_IOS(3_0);
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event NS_AVAILABLE_IOS(3_0);
motionEnded方法中处理:
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
if (motion == UIEventSubtypeMotionShake)
{
// FlyElephant http://www.cnblogs.com/xiaofeixiang
[[NSNotificationCenter defaultCenter] postNotificationName:@"FlyElephant" object:self];
}
}
CoreMotion在处理加速计数据和陀螺仪数据的时是一个非常重要的框架,框架本身集成了很多算法获取原生的数据,而且能很好的展现出来,CoreMotion与UIKit不同,连接的是UIEvent而不是事件响应链。CoreMotion相对于接收数据只是更简单的分发motion事件。
CMMotionManager类能够使用到设备的所有移动数据(motion data),Core Motion框架提供了两种对motion数据的操作方式:
pull方式:能够以CoreMotionManager的只读方式获取当前任何传感器状态或是组合数据;
push方式:是以块或者闭包的形式收集到想要得到的数据并且在特定周期内得到实时的更新;
pull处理方式:
//判断加速计是否可用
if ([_motionManager isAccelerometerAvailable]) {
// 设置加速计采样频率
[_motionManager setAccelerometerUpdateInterval:1 / 40.0];
[_motionManager startAccelerometerUpdates];
} else {
NSLog(@"博客园-FlyElephant");
}
触摸结束:
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
CMAcceleration acceleration=_motionManager.accelerometerData.acceleration;
NSLog(@"%f---%f---%f",acceleration.x,acceleration.y,acceleration.z);
}
push处理方式:
@property (strong,nonatomic) CMMotionManager *motionManager; @property (strong,nonatomic) NSOperationQueue *quene;
_motionManager=[[CMMotionManager alloc]init];
//判断加速计是否可用
if ([_motionManager isAccelerometerAvailable]) {
// 设置加速计频率
[_motionManager setAccelerometerUpdateInterval:1 / 40.0];
//开始采样数据
[_motionManager startAccelerometerUpdatesToQueue:_quene withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) {
NSLog(@"%f---%f",accelerometerData.acceleration.x,accelerometerData.acceleration.y);
}];
} else {
NSLog(@"博客园-FlyElephant");
}
时间设置频率:
陀螺仪
陀螺仪其实主要方法和方式和加速计没有区别,先看张陀螺仪旋转的角度图片:
陀螺仪更新数据也有两种方式,pull方式(startGyroUpdates),push方式(startGyroUpdatesToQueue):
static const NSTimeInterval gyroMin = 0.01; - (void)startUpdatesWithSliderValue:(int)sliderValue { // Determine the update interval
NSTimeInterval delta = 0.005;
NSTimeInterval updateInterval = gyroMin + delta * sliderValue; // Create a CMMotionManager
CMMotionManager *mManager = [(APLAppDelegate *)[[UIApplication sharedApplication] delegate] sharedManager];
APLGyroGraphViewController * __weak weakSelf = self; // Check whether the gyroscope is available
if ([mManager isGyroAvailable] == YES) {
// Assign the update interval to the motion manager
[mManager setGyroUpdateInterval:updateInterval];
[mManager startGyroUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMGyroData *gyroData, NSError *error) {
[weakSelf.graphView addX:gyroData.rotationRate.x y:gyroData.rotationRate.y z:gyroData.rotationRate.z];
[weakSelf setLabelValueX:gyroData.rotationRate.x y:gyroData.rotationRate.y z:gyroData.rotationRate.z];
}];
}
self.updateIntervalLabel.text = [NSString stringWithFormat:@"%f", updateInterval];
} - (void)stopUpdates{
CMMotionManager *mManager = [(APLAppDelegate *)[[UIApplication sharedApplication] delegate] sharedManager];
if ([mManager isGyroActive] == YES) {
[mManager stopGyroUpdates];
}
}
很晚了,先睡觉了~有空补充一下~
iOS开发-CoreMotion框架(加速计和陀螺仪)的更多相关文章
- iOS开发-CoreMotion框架
转自: CoreMotion是一个专门处理Motion的框架,其中包含了两个部分 加速度计和陀螺仪,在iOS4之前加速度计是由 UIAccelerometer 类 来负责采集数据,现在一般都是用Cor ...
- iOS学习新知识-加速计和陀螺仪
一.CoreMotion框架介绍 我们知道有一些iOS的应用,会有一些特殊的要求,比如: 电子罗盘指南针之类的应用:让我们知道方向. 运动类型软件:让我们知道我们跑步多少公里. 社交软件中的摇一摇功能 ...
- iOS开发基础框架
---恢复内容开始--- //appdelegate //// AppDelegate.m// iOS开发架构//// Copyright © 2016年 Chason. All rights ...
- iOS开发 传感器(加速计、摇一摇、计步器)
一.传感器 1.什么是传感器传感器是一种感应\检测周围环境的一种装置, 目前已经广泛应用于智能手机上 传感器的作用用于感应\检测设备周边的信息不同类型的传感器, 检测的信息也不一样 iPhone中的下 ...
- iOS开发-网络框架-b
网络框架(以下称NJAFNetworking)是基于AFNetworking框架的简单封装,基本功能包括POST请求,GET请求,上传文件,下载文件,网络状态,缓存等. 为什么要使用NJAFNetwo ...
- iOS开发UIKit框架-可视化编程-XIB
1. Interface Builder 可视化编程 1> 概述 GUI : 图形用户界面(Graphical User Interface, 简称GUI, 又称图形化界面) 是指采用图形方式显 ...
- 简单搭建iOS开发项目框架
今天我们来谈谈如何搭建框架,框架需要做一些什么. 第一步:找到我们的目标我们的目标是让其他开发人员拿到手后即可写页面,不再需要考虑其他的问题. 第二步:我们需要做哪些东西各位跟着我一步一步来进行. 假 ...
- IOS开发 基础框架(Fondation Framework)的线程安全
有一种误解,认为基础框架(Foundation framework)是线程安全的,而Application Kit是非线程安全的.不幸的是,这是一个总的概括,从而造成一点误导.每个框架都包含了线程安全 ...
- iOS开发 - CoreData框架 数据持久化
Core Data Core Data是iOS5之后才出现的一个框架,它提供了对象-关系映射(ORM)的功能,即能够将OC对象转化成数据,保存在SQLite数据库文件中,也能够将保存在数据库中的数据还 ...
随机推荐
- PHP 数组的添加和读取
在实际的开发中,会经常使用数组的添加和读取.这里把经常使用的操作记下来,以备以后查阅. <?php //一维数值数组 $list = array('wang','god'); $list[] = ...
- CSUOJ 1726 你经历过绝望吗?两次!BFS+优先队列
Description 4月16日,日本熊本地区强震后,受灾严重的阿苏市一养猪场倒塌,幸运的是,猪圈里很多头猪依然坚强存活.当地15名消防员耗时一天解救围困的"猪坚强".不过与在废 ...
- libhiredis.so.0.13: cannot open shared object file: No such file or director
Hiredis安装步骤: tar zxvf antirez-hiredis-v0.10.1-0-g3cc6a7f.zip cd antirez-hiredis-3cc6a7f make 解决办法: m ...
- MongoDB复制原理
##mongodb复制(主从服务器数据备份, 一个主服务器可以有很多个从服务器) #mongodb的复制至少需要两个节点.其中一个是主节点,负责处理客户端请求,其余的都是从节点,负责复制主节点上的数据 ...
- 构造Huffman以及实现
构造Huffman 题目 在作业本上分别针对权值集合W=(6,5,3,4,60,18,77)和W=(7,2,4,5,8)构造哈夫曼树,提交构造过程的照片 错误回答 错误原因:遵循左边小于根右边大于根的 ...
- window.open如何返回值给父窗口
父窗口代码 function showMyWindowNew() { var iTop = (window.screen.availHeight - 30 - 450) / 2; //获得窗口的水平位 ...
- Oracle SP2-0640
安装Oracle database 11g express edition后,使用自带的SQL命令行,执行 select 1 from dual; 报出错误:SP2-0640 未连接 解决方法:使用 ...
- PostgreSQL远程连接配置管理/账号密码分配(解决:致命错误: 用户 "postgres" Ident 认证失败)
问题:致命错误: 用户 "postgres" Ident 认证失败 说明:这个是由于没有配置远程访问且认证方式没改造成的,只需要更改使用账号密码认证即可. 解决:找到pg_hba. ...
- POJ 1470 Closest Common Ancestors (LCA,离线Tarjan算法)
Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000K Total Submissions: 13372 Accept ...
- gcc g++支持C++11 标准编译及其区别
g++ -g -Wall -std=c++11 main.cpp gcc -g -Wall -std=c11 main.cpp 如果不想每次写这个-std=C++11这个选项该怎么办呢? 方法出处:h ...