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中不是任何对象都能处理事 ...
随机推荐
- TCP & UDP & IP
TCP和UDP的区别 TCP UDP 是否连接 面向连接 面向非连接 应用场合 可靠的 不可靠的 速度 慢 快 传送数据 字节流 数据报 是否可用于广播 否 是 为什么UDP比TCP快 不需要连接 ...
- Mysql中的视图
什么是视图 通俗的讲,视图就是一条SELECT语句执行后返回的结果集.所以我们在创建视图的时候,主要的工作就落在创建这条SQL查询语句上. 视图的特性 视图是对若干张基本表的引用,一张虚表,查询语句执 ...
- mysql登录和连接 权限
在一些配置中会要求登录mysql 授权的时候注意ip地址是ip地址,localhost是localhost,在grant授权时,如果用localhost,就必须在所登录的配置文件中使用localhos ...
- 一个人的Scrum之准备工作
在2012年里,我想自己一人去实践一下Scrum,所以才有了这么一个开篇. 最近看了<轻松的Scrum之旅>这本书,感觉对我非常有益.书中像讲述故事一样描述了在执行Scrum过程中的点点滴 ...
- JavaScript中的不可见数据类型
JS提供了一些内置对象.函数和构造器供我们编程,如Math.parseInt.Object.Array等.这些都是可见的,编程时可以使用的.比如我可以new Object 或 new Array. 有 ...
- 计算几何--求凸包模板--Graham算法--poj 1113
Wall Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 28157 Accepted: 9401 Description ...
- 10个值得深思的PHP面试问题
本文所罗列的问题虽然看似简单,但是每个背后都涵盖了一个或几个大家容易忽视的基础知识点,希望能够帮助到你的面试和平时工作. Q1 第一个问题关于弱类型 $str1 = 'yabadabadoo'; $s ...
- Storm 基础知识
分布式的实时计算框架,storm对于实时计算的意义类似于hadoop对于批处理的意义. Storm的适用场景: 1.流数据处理:storm可以用来处理流式数据,处理之后将结果写到某个存入中去. 2.持 ...
- 有限状态机HDL模板
逻辑设计, 顾名思义, 只要理清了逻辑和时序, 剩下的设计只是做填空题而已. 下面给出了有限状态机的标准设计,分别为 VHDL 和 Verilog 代码 1 有限状态机 2 VHDL模板之一 li ...
- hdu-5834 Magic boy Bi Luo with his excited tree(树形dp)
题目链接: Magic boy Bi Luo with his excited tree Time Limit: 8000/4000 MS (Java/Others) Memory Limit: ...