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中不是任何对象都能处理事 ...
随机推荐
- 【MySQL】MySQL忘记root密码解决方案
转眼间从实习到现在已经快两年了.两年的工作做遇到过很多很多的拦路虎,大部分也通过搜索引擎找到了解决的方案.奈何大脑不是硬盘,偶尔有的问题第二次遇到还是有点抓蒙...所以决定把这些东西记录在博客上.这样 ...
- 【JavaScript】JQuery中$.fn、$.extend、$.fn.extend
Web开发肯定要使用第三方插件,对于一个炫丽的效果都忍不住想看看对方是如何实现的,刚下载了一个仿京东商品鼠标经过时局部放大的插件.看了两眼JQuery源码,看看就感觉一头雾水.JQuery本来自己学的 ...
- Winpcap
Winpcap网络开发库入门
- C# listview 拖动节点
/// <summary> /// 当拖动某项时触发 /// </summary> /// <param name="sender"></ ...
- tmpFile.renameTo(classFile) failed 错误
突然的出现了这个tmpFile.renameTo(classFile) failed 错误. 也许是我删掉了tomcat里面的webapps 中的项目,而通过debug部署,而出现了这个问题. 一开始 ...
- [转]Android输出Log到文件
前言:开发中遇到mx4这款机型Eclipse联调不上,logcat看不了,需要输出生成文件查看调试信息.网上搜了下,功能很完善了.startService和过滤输出信息需要自己添加设置,另外注意添加权 ...
- linux内核宏container_of前期准备之gcc扩展关键字typeof
typeof基本介绍 typeof(x) 这是它的使用方法,x可以是数据类型或者表达式.它的作用时期和sizeof类似,就是它是在编译器从高级语言(如C语言)翻译成汇编语言时起作用,这个很重要,稍后会 ...
- SQL Server with(nolock)详解
大家在写查询时,为了性能,往往会在表后面加一个nolock,或者是with(nolock),其目的就是查询是不锁定表,从而达到提高查询速度的目的. 什么是并发访问:同一时间有多个用户访问同一资源,并发 ...
- hadoop debug script
A Hadoop job may consist of many map tasks and reduce tasks. Therefore, debugging a Hadoop job is of ...
- jquery 实现邮箱输入自动提示功能:(一)
记得去年做某个项目的时候,用到了邮箱输入自动提示功能,于是网上搜了一下,发现了这个写得不错,现在回想起来,转载一下,方便查阅. 邮箱的广泛使用得益于它的免费,因此很多网站在注册的时候都会直接使用邮箱作 ...