Core Motion传感器原始数据
1、访问原始的Motion数据 #import <UIKit/UIKit.h>
#import <CoreMotion/CoreMotion.h> @interface ViewController : UIViewController @property (strong, nonatomic) IBOutlet UILabel *xAccLabel;
@property (strong, nonatomic) IBOutlet UILabel *yAccLabel;
@property (strong, nonatomic) IBOutlet UILabel *zAccLabel;
@property (strong, nonatomic) IBOutlet UILabel *xGyroLabel;
@property (strong, nonatomic) IBOutlet UILabel *yGyroLabel;
@property (strong, nonatomic) IBOutlet UILabel *zGyroLabel;
@property (strong, nonatomic) IBOutlet UILabel *xMagLabel;
@property (strong, nonatomic) IBOutlet UILabel *yMagLabel;
@property (strong, nonatomic) IBOutlet UILabel *zMagLabel; @property (nonatomic, strong) CMMotionManager * motionManager; - (void)startUpdates;
- (void)stopUpdates;
- (CMMotionManager *)motionManager
{
if (_motionManager == nil)
{
_motionManager = [[CMMotionManager alloc] init];
}
return _motionManager;
} - (void)startUpdates
{
// Start accelerometer if available
if ([self.motionManager isAccelerometerAvailable])
{
[self.motionManager setAccelerometerUpdateInterval:1.0/2.0]; //Update twice per second
[self.motionManager startAccelerometerUpdatesToQueue:[NSOperationQueue mainQueue]
withHandler:
^(CMAccelerometerData *data, NSError *error)
{
self.xAccLabel.text = [NSString stringWithFormat:@"%f", data.acceleration.x];
self.yAccLabel.text = [NSString stringWithFormat:@"%f", data.acceleration.y];
self.zAccLabel.text = [NSString stringWithFormat:@"%f", data.acceleration.z];
}];
} // Start gyroscope if available
if ([self.motionManager isGyroAvailable])
{
[self.motionManager setGyroUpdateInterval:1.0/2.0]; //Update twice per second
[self.motionManager startGyroUpdatesToQueue:[NSOperationQueue mainQueue]
withHandler:
^(CMGyroData *data, NSError *error)
{
self.xGyroLabel.text = [NSString stringWithFormat:@"%f", data.rotationRate.x];
self.yGyroLabel.text = [NSString stringWithFormat:@"%f", data.rotationRate.y];
self.zGyroLabel.text = [NSString stringWithFormat:@"%f", data.rotationRate.z];
}];
} // Start magnetometer if available
if ([self.motionManager isMagnetometerAvailable])
{
[self.motionManager setMagnetometerUpdateInterval:1.0/2.0]; //Update twice per second
[self.motionManager startMagnetometerUpdatesToQueue:[NSOperationQueue mainQueue]
withHandler:
^(CMMagnetometerData *data, NSError *error)
{
self.xMagLabel.text = [NSString stringWithFormat:@"%f", data.magneticField.x];
self.yMagLabel.text = [NSString stringWithFormat:@"%f", data.magneticField.y];
self.zMagLabel.text = [NSString stringWithFormat:@"%f", data.magneticField.z];
}];
}
} -(void)stopUpdates
{
if ([self.motionManager isAccelerometerAvailable] && [self.motionManager isAccelerometerActive])
{
[self.motionManager stopAccelerometerUpdates];
} if ([self.motionManager isGyroAvailable] && [self.motionManager isGyroActive])
{
[self.motionManager stopGyroUpdates];
} if ([self.motionManager isMagnetometerAvailable] && [self.motionManager isMagnetometerActive])
{
[self.motionManager stopMagnetometerUpdates];
} }
- (void)applicationWillResignActive:(UIApplication *)application
{
[self.viewController stopUpdates];
} - (void)applicationDidBecomeActive:(UIApplication *)application
{
[self.viewController startUpdates];
}
2、访问设备的Motion数据
#import <UIKit/UIKit.h>
#import <CoreMotion/CoreMotion.h> @interface ViewController : UIViewController @property (strong, nonatomic) IBOutlet UILabel *rollLabel;
@property (strong, nonatomic) IBOutlet UILabel *pitchLabel;
@property (strong, nonatomic) IBOutlet UILabel *yawLabel;
@property (strong, nonatomic) IBOutlet UILabel *xRotLabel;
@property (strong, nonatomic) IBOutlet UILabel *yRotLabel;
@property (strong, nonatomic) IBOutlet UILabel *zRotLabel;
@property (strong, nonatomic) IBOutlet UILabel *xGravLabel;
@property (strong, nonatomic) IBOutlet UILabel *yGravLabel;
@property (strong, nonatomic) IBOutlet UILabel *zGravLabel;
@property (strong, nonatomic) IBOutlet UILabel *xAccLabel;
@property (strong, nonatomic) IBOutlet UILabel *yAccLabel;
@property (strong, nonatomic) IBOutlet UILabel *zAccLabel;
@property (strong, nonatomic) IBOutlet UILabel *xMagLabel;
@property (strong, nonatomic) IBOutlet UILabel *yMagLabel;
@property (strong, nonatomic) IBOutlet UILabel *zMagLabel; @property (nonatomic, strong) CMMotionManager *motionManager; - (void)startUpdates;
- (void)stopUpdates; @end
- (CMMotionManager *)motionManager
{
// Lazy initialization
if (_motionManager == nil)
{
_motionManager = [[CMMotionManager alloc] init];
}
return _motionManager;
} - (void)startUpdates
{
// Start device motion updates
if ([self.motionManager isDeviceMotionAvailable])
{
//Update twice per second
[self.motionManager setDeviceMotionUpdateInterval:1.0/2.0];
[self.motionManager startDeviceMotionUpdatesUsingReferenceFrame:
CMAttitudeReferenceFrameXMagneticNorthZVertical
toQueue:[NSOperationQueue mainQueue]
withHandler:
^(CMDeviceMotion *deviceMotion, NSError *error)
{
// Update attitude labels
self.rollLabel.text = [NSString stringWithFormat:@"%f", deviceMotion.attitude.roll];
self.pitchLabel.text = [NSString stringWithFormat:@"%f", deviceMotion.attitude.pitch];
self.yawLabel.text = [NSString stringWithFormat:@"%f", deviceMotion.attitude.yaw]; // Update rotation rate labels
self.xRotLabel.text = [NSString stringWithFormat:@"%f", deviceMotion.rotationRate.x];
self.yRotLabel.text = [NSString stringWithFormat:@"%f", deviceMotion.rotationRate.y];
self.zRotLabel.text = [NSString stringWithFormat:@"%f", deviceMotion.rotationRate.z]; // Update user acceleration labels
self.xGravLabel.text = [NSString stringWithFormat:@"%f", deviceMotion.gravity.x];
self.yGravLabel.text = [NSString stringWithFormat:@"%f", deviceMotion.gravity.y];
self.zGravLabel.text = [NSString stringWithFormat:@"%f", deviceMotion.gravity.z]; // Update user acceleration labels
self.xAccLabel.text = [NSString stringWithFormat:@"%f", deviceMotion.userAcceleration.x];
self.yAccLabel.text = [NSString stringWithFormat:@"%f", deviceMotion.userAcceleration.y];
self.zAccLabel.text = [NSString stringWithFormat:@"%f", deviceMotion.userAcceleration.z]; // Update magnetic field labels
self.xMagLabel.text = [NSString stringWithFormat:@"%f", deviceMotion.magneticField.field.x];
self.yMagLabel.text = [NSString stringWithFormat:@"%f", deviceMotion.magneticField.field.y];
self.zMagLabel.text = [NSString stringWithFormat:@"%f", deviceMotion.magneticField.field.z];
}];
}
} -(void)stopUpdates
{
if ([self.motionManager isDeviceMotionAvailable] && [self.motionManager isDeviceMotionActive])
{
[self.motionManager stopDeviceMotionUpdates];
}
}
- (void)applicationWillResignActive:(UIApplication *)application
{
[self.viewController stopUpdates];
} - (void)applicationDidBecomeActive:(UIApplication *)application
{
[self.viewController startUpdates];
}
Core Motion传感器原始数据的更多相关文章
- IOS 特定于设备的开发:Core Motion基础
Core Motion框架集中了运动数据处理.该框架是在IOS 4 SDK中引入的,用于取代accelerometer加速计访问.它提供了对3个关键的机载传感器的集中式监测.这些传感器有陀螺仪.磁力计 ...
- IOS Core Motion、UIAccelerometer(加速计使用)
加速计 ● 加速计的作用 ● 用于检测设备的运动(比如摇晃) ● 加速计的经典应用场景 ● 摇一摇 ● 计步器 ● 加速计程序的开发 ● 在iOS4以前:使用UIAccelerometer,用法非常简 ...
- iOS开发 传感器(加速计、摇一摇、计步器)
一.传感器 1.什么是传感器传感器是一种感应\检测周围环境的一种装置, 目前已经广泛应用于智能手机上 传感器的作用用于感应\检测设备周边的信息不同类型的传感器, 检测的信息也不一样 iPhone中的下 ...
- iOS开发——高级篇——传感器(加速计、摇一摇、计步器)
一.传感器 1.什么是传感器传感器是一种感应\检测周围环境的一种装置, 目前已经广泛应用于智能手机上 传感器的作用用于感应\检测设备周边的信息不同类型的传感器, 检测的信息也不一样 iPhone中的下 ...
- iOS 神秘而又强大的传感器系统 (附demo)
iOS中的各种传感器: 随着科技的发展,机器感知人的行为!Goole的无人驾驶汽车到李彦宏的无人驾汽车,都带入了各种计算及传感. 为了研究自然现象和制造劳动工具,人类必须了解外界的各类信息.了解外界信 ...
- iOS 传感器集锦
https://www.jianshu.com/p/5fc26af852b6 传感器集锦:指纹识别.运动传感器.加速计.环境光感.距离传感器.磁力计.陀螺仪 效果预览.gif 一.指纹识别 应用: ...
- 【读书笔记】iOS-使用传感器
和其他的高端智能机一样,iPhone携带了很多传感器:照相机,加速度计,GPS模块和数字指南针. 使用Core Motion框架,你的应用可以读取来自于加速度计,磁力计以及陀螺仪的运动数据. 近距离传 ...
- Core Services层
本文转载至 http://jingyan.baidu.com/article/cdddd41c57360853cb00e124.html Core Services层是系统很多部分的基础部分,也许应用 ...
- iOS---开发实用传感器
传感器 1.什么是传感器 传感器是一种感应\检测装置, 目前已经广泛应用于智能手机上 2.传感器的作用 用于感应\检测设备周边的信息 不同类型的传感器, 检测的信息也不一样 iPhone中的下面现象都 ...
随机推荐
- python面向对象编程实例解析
1. 类和函数 面向对象编程的例子: #!/usr/bin/env python # -*- coding: utf-8 -*- class Person(object): #在属性和变量的前面增加“ ...
- keybd_event函数用法
转自不用winio,直接达到驱动级模拟键盘效果的keybd_event函数用法 键盘模拟技术是在编写游戏外挂时经常使用的技术.但是由于很多游戏采用了directinput的方式,使得发送的一般键盘消息 ...
- IntelliJ IDEA 创建web项目后添加Java EE (Tomcat)的依赖包
本文讲述的是IntelliJ IDEA 12版本 如果在编译器中创建一个web项目后,没有设置tomcat的依赖包,就不能成功的编译,会缺少javax.servlet.*等类. 添加的方法是: 打开p ...
- 如何用Python从本地将一个文件备份到Google Drive
1.要有一个Google App账号: 这个可以上网上去申请,申请地址为:https://developers.google.com/appengine/?hl=zh-cn 2.创建一个Google ...
- Linux本地无法登录,远程却可以登录
[root@oraserver ~]# vi /etc/pam.d/login 将以下内容注释掉: #session required /lib/security/pam_limits. ...
- op论坛,分支
http://www.arm9home.net/thread.php?fid=68 http://www.openwrt.org.cn/bbs/forum.php https://dev.openwr ...
- SPOJ_10628_Count_on_a_Tree_(主席树+Tarjan)
描述 http://www.spoj.com/problems/COT/ 给出一棵n个节点的树,树上每一个节点有权值.m次询问,求书上u,v路径中第k小的权值. COT - Count on a tr ...
- 【转】关于Certificate、Provisioning Profile、App ID的介绍及其之间的关系
原文网址:http://www.cnblogs.com/cywin888/p/3263027.html 刚接触iOS开发的人难免会对苹果的各种证书.配置文件等不甚了解,可能你按照网上的教程一步一步的成 ...
- DELL笔记本拆机添加内存条
在笔记本后面拧开7个螺丝 然后打开后盖 掰开卡口,内存条会弹出,此时按住内存条两侧的缺口往外用力就可以拔出内存条. 装入内存条时,先插入内存条,按下即可.
- nyoj 37回文串
述所谓回文字符串,就是一个字符串,从左到右读和从右到左读是完全一样的,比如"aba".当然,我们给你的问题不会再简单到判断一个字符串是不是回文字符串.现在要求你,给你一个字符串,可 ...