iOS新加速计事件(陀螺仪和加速计)
【iOS新加速计事件】
1、iOS5.0以前,可以使用UIAcceleration来监听加速计事件。
2、Bug iOS5.0以后,UIAccelerometerDelegate已经被depreacated,如下:

deprecated不是说不能说了,而是意味着在将来版本会删除,所以如果不想更新知识的话,就使用UIAccelerometer吧。更保险的方法是使用一个Timer来检查UIAcceleration,即不依赖于此Delegate回调。
3、针对iOS4.0以上版本,推荐使用CMMotionManager来监听加速计事件。涉及到下面几个方法:


4、其实,CMMotionManager也挺简单的,加速计的方法就这么几个。
陀螺仪(传感器)和加速计:
加速计和陀螺仪的值都是通过Core Motion框架访问的,此框架提供了CMMotionManager类。该类提供的所有数据都是用来描述用户如何移动设备的。
应用程序创建一个CMMotionManager实例,然后通过以下某种模式使用它:
1它可以在动作发生时执行一些代码
2 它可以时刻监视一个持续更新的结构,使你随时能够访问最新的值。
陀螺仪(传感器)和加速计的使用:
label属性检查器的Lines改为0,高度增加即可。
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (weak,nonatomic) IBOutlet UILabel *accelerometerLabel;
@property (weak,nonatomic) IBOutlet UILabel *gyroscopeLabel;
@end
.m文件:
#import "ViewController.h"
#import <CoreMotion/CoreMotion.h>
@interface ViewController ()
@property (strong,nonatomic) CMMotionManager *motionManager;
//@property (strong,nonatomic) NSOperationQueue *queue;//第一种
@property (strong,nonatomic) NSTimer *updateTime;
@end
@implementation ViewController
-(NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskPortrait;
}
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[self.motionManager startAccelerometerUpdates];
[self.motionManager startGyroUpdates];
self.updateTime = [NSTimer scheduledTimerWithTimeInterval:1.0/10.0 target:self selector:@selector(updateDisplay) userInfo:nil repeats:YES];
}
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
[self.motionManager stopAccelerometerUpdates];
[self.motionManager stopGyroUpdates];
[self.updateTime invalidate];
self.updateTime = nil;
}
-(void)updateDisplay{
if (self.motionManager.accelerometerAvailable) {
CMAccelerometerData *accelerometerData = self.motionManager.accelerometerData;
self.accelerometerLabel.text =[NSString stringWithFormat:@"Accelerometer\n---\n""x: %+.2f\ny: %+.2f\nz: %+.2f",accelerometerData.acceleration.x,accelerometerData.acceleration.y,accelerometerData.acceleration.z];
}
if (self.motionManager.gyroAvailable) {
CMGyroData *gyroData = self.motionManager.gyroData;
self.gyroscopeLabel.text = [NSString stringWithFormat:@"Gyroscope\n---\n""x: %+.2f\ny: %+.2f\nz: %+.2f",gyroData.rotationRate.x,gyroData.rotationRate.y,gyroData.rotationRate.z];
}
}
-(void)viewDidLoad{
[super viewDidLoad];
self.motionManager = [[CMMotionManager alloc]init];//动作管理器初始化
if (self.motionManager.accelerometerAvailable) {
self.motionManager.accelerometerUpdateInterval = 1.0/10.0; //0.1s更新一次
}else{
self.accelerometerLabel.text = @"This device has no accelerometer.";
}
if (self.motionManager.gyroAvailable) {
self.motionManager.gyroUpdateInterval = 1.0/10.0;
}else{
self.accelerometerLabel.text = @"This device has no Gyroscope.";
}
/*
self.queue = [[NSOperationQueue alloc]init];
if (self.motionManager.isAccelerometerAvailable) {//判断有没有加速计
self.motionManager.accelerometerUpdateInterval = 1.0/10.0; //0.1s更新一次
//告诉动作管理器开始报告加速计更新
[self.motionManager startAccelerometerUpdatesToQueue:self.queue withHandler:^(CMAccelerometerData * _Nullable accelerometerData, NSError * _Nullable error) {
NSString *labelText;
if (error) {
[self.motionManager stopAccelerometerUpdates];
labelText = [NSString stringWithFormat:@"Accelerometer encounted error:%@",error];
}else{
labelText = [NSString stringWithFormat:@"Accelerometer\n---\n""x: %+.2f\ny: %+.2f\nz: %+.2f",accelerometerData.acceleration.x,accelerometerData.acceleration.y,accelerometerData.acceleration.z];
}
dispatch_async(dispatch_get_main_queue(),^{
self.accelerometerLabel.text = labelText;
});
}];
}else {
self.accelerometerLabel.text = @"This device has no accelerometer.";
}
if (self.motionManager.gyroAvailable) {//判断有没有陀螺仪
self.motionManager.gyroUpdateInterval = 1.0/10.0;
[self.motionManager startGyroUpdatesToQueue:self.queue withHandler:^(CMGyroData * _Nullable gyroData, NSError * _Nullable error) {
NSString *labelText;
if (error) {
[self.motionManager stopGyroUpdates];
labelText = [NSString stringWithFormat:@"Gyroscope encounted error:%@",error];
}else{
labelText = [NSString stringWithFormat:@"Gyroscope\n---\n""x: %+.2f\ny: %+.2f\nz: %+.2f",gyroData.rotationRate.x,gyroData.rotationRate.y,gyroData.rotationRate.z];
}
dispatch_async(dispatch_get_main_queue(),^{
self.gyroscopeLabel.text = labelText;
});
}];
}else{
self.accelerometerLabel.text = @"This device has no Gyroscope.";
}
*/
}
@end
iOS新加速计事件(陀螺仪和加速计)的更多相关文章
- iOS开发——高级篇——传感器(加速计、摇一摇、计步器)
一.传感器 1.什么是传感器传感器是一种感应\检测周围环境的一种装置, 目前已经广泛应用于智能手机上 传感器的作用用于感应\检测设备周边的信息不同类型的传感器, 检测的信息也不一样 iPhone中的下 ...
- cocos2d-x 事件分发机制 ——加速计事件监听
加速计事件监听机制 在上一篇中介绍了cocos2d-x中的触摸事件机制,这篇来介绍下游戏中也常常常使用到的加速计事件,这些都是游戏中的常常要用到的. 移动设备上一个非常重要的输入源是设备的方向.大多数 ...
- iOS中的事件响应链、单例模式、工厂模式、观察者模式
学习内容 欢迎关注我的iOS学习总结--每天学一点iOS:https://github.com/practiceqian/one-day-one-iOS-summary iOS中事件传递和相应机制 i ...
- 1.0 iOS中的事件
本文并非最终版本,如有更新或更正会第一时间置顶,联系方式详见文末 如果觉得本文内容过长,请前往本人 “简书” 在用户使用app过程中,会产生各种各样的事件,iOS中的事件可以分为3大类型: UIK ...
- iOS中—触摸事件详解及使用
iOS中--触摸事件详解及使用 (一)初识 要想学好触摸事件,这第一部分的基础理论是必须要学会的,希望大家可以耐心看完. 1.基本概念: 触摸事件 是iOS事件中的一种事件类型,在iOS中按照事件划分 ...
- iOS基础 - 触摸事件与手势识别
一.iOS的输入事件 UIKit可识别三种类型的输入事件: 触摸事件 运动(加速计)事件 远程控制事件 二.UIEvent iOS中许多事件对象都是UIEvent类的实例,记录事件产生的时刻和类型 U ...
- 一、iOS中的事件可以分为3大类型
触摸事件加速计事件远程控制事件 响应者对象在iOS中不是任何对象都能处理事件,只有继承了UIResponder的对象才能接收并处理事件.我们称之为"响应者对象" UIApplica ...
- 响应者链条,iOS中touchs事件的处理流程。
用户在使用app的时候,会产生各样的事件.在iOS中的事件可以分为三种 触摸事件(Touch Event) 加速计事件(Accelerometer Event) 远程控制事件(Remote Contr ...
- iOS 点击事件传递及响应
1.iOS中的事件 iOS中的事件可以分为3大类型: 触摸事件 加速计事件 远程控制事件这里我们只讨论iOS中的触摸事件. 1.1响应者对象(UIResponder) 在iOS中不是任何对象都能处理事 ...
随机推荐
- Effective Java 14 In public classes, use accessor methods, not public fields
Principle To offer the benefits of encapsulation you should always expose private field with public ...
- eclipse发布项目时,会自动还原server.xml和content.xml文件
因为Tomcat的端口冲突,导致eclipse发布项目时,失败.于是到server.xml文件中修改端口,重启使用eclipse发布项目,发现依然报端口冲突的错误,其原因时,刚才对server.xml ...
- JavaScript字符串函数大全
JS自带函数concat将两个或多个字符的文本组合起来,返回一个新的字符串.var a = "hello";var b = ",world";var c = a ...
- device eth0 does not seem to be present, delaying initialization
在搭建LVS+Keepalived高可用负载均衡环境的过程中,使用VirtualBox复制了两个Centos的环境,并且选中了“重新初始化网卡”的选项,但是在启动这两个复制的Centos环境的时候,发 ...
- 【nginx】常见的陷阱和错误
很多人都可以碰到一个陷阱.下面我们列出,我们经常看到的问题,以及解释如何解决这些问题.在Freenode上的#nginx IRC频道这些讨论很频繁. 1.权限 从来不要使用777权限,查看目录的权限 ...
- IIS does not list a website that matches the launch url
233down voteaccepted I hate answering my questions: in my question i stated that i was running VS un ...
- EXECL文件导入数据库
Execl数据导入数据库: 注意事项:execl中的列名与列数要与数据库的列名和列数一致.值类型一致,列名不一致的话可在导入的时候,给字段起别名,确定保持一致 v 界面代码: <div> ...
- 20150912华为机考1之"输入一个字符串,将其中出现次数最多的字符输出"
不吐槽华为的服务器了,直接上正文 输入:字符串(英文字母),长度不超过128 输出:出现频率最高的字母 思路写在注释文档 /* Input a string * Output the most fre ...
- C++ inline
内联函数相对于宏的区别和优点: 宏是在预处理时进行的机械替换,内联是在编译时进行的.内联函数是真正的函数,只是在调用时,没有调用开销,像宏一样进行展开.内联函数会进行参数匹配检查,相对于带参数的宏有很 ...
- Medial Queries的另一用法:实现IE hack的方法
所谓Medial Queries就是媒体查询. 随着Responsive设计的流行,Medial Queries可算是越来越让人观注了.他可以让Web前端工程实现不同设备下的样式选择,让站点在不同的设 ...