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中不是任何对象都能处理事 ...
随机推荐
- [转]Linux下的Makefile
Makefile 介绍——————— make命令执行时,需要一个 Makefile 文件,以告诉make命令需要怎么样的去编译和链接程序. 首先,我们用一个示例来说明Makefile的书写规则.以便 ...
- Android Design Support Library——TabLayout
TabLayout——选项卡布局,通过选项卡的方式切换view并不是material design中才有的新概念,选项卡既可以固定,也可以滚动显示效果如下: 通过addTab方法可以实现选项卡的动态添 ...
- Effective Java 08 Obey the general contract when overriding equals
When it's the case that each instance of the class is equal to only itself. 1. Each instance of the ...
- Linux 之创建工作目录-mkdir
在Linux下创建工作目录,一般使用 "mkdir" 指令,一下将介绍"mkdir"指令的使用方法,供大家参考. 一.使用帮助 在Linux终端(命令行)输入: ...
- 学完STM32开发板,就选4412开发板让你有目标的学习嵌入式开发
600余页用户使用手册 linux实验手册(资料不断更新)100期配套零基础高清视频教程 轻松入门 (资料不断更新)2000人售后认证群 在线支持 售后无忧 源码全开源 原厂技术资料经典学习书籍推荐 ...
- repcached的安装练习
1 自行寻找一个具有大量非结构化数据,很难使用关系型数据库进行处理的场景,清晰描述场景并指出困难所在,要求原创 譬如说:以易迅电商为例,顾客会对购买的商品做出反馈评论,这些评论都是非结构化的数据,如 ...
- perl学习笔记
一.正则表达式 匹配一个文件中的某个单词,并打印出来 #!/usr/bin/perl use strict; use warnings; ; open(FILE, "< temp.pl ...
- 【C#】SQL数据库助手类2.0(自用)
using System; using System.Collections.Generic; using System.Configuration; using System.Data; using ...
- [3D跑酷] AudioManager
Unity音频管理 游戏中的声音管理最常用的组件莫过于AudioSource和AudioClip,我的做法是建立是一个AudioManager类(单例类)管理各个音频,谈一下我的经验: 函数列表 St ...
- IO流的练习3 —— 复制多级文件夹下的指定文件并改名
需求:复制指定目录下的指定文件,并修改后缀名. 指定的文件是:.java文件. 指定的后缀名是:.jad 数据源所在文件夹:C:\Users\Administrator\Desktop\记录 目的地所 ...