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中的下面现象都 ...
随机推荐
- BZOJ 1634: [Usaco2007 Jan]Protecting the Flowers 护花
Description Farmer John went to cut some wood and left N (2 <= N <= 100,000) cows eating the g ...
- 解决DBCP报错 Could not retrieve transation read-only s
dbcp连接池报错 commons-dbcp 解决Mysql Cannot get a connection, pool error: Could not create a validated ob ...
- javascript mvc
http://www.webjx.com/javascript/jsajax-15996.html http://blog.csdn.net/jjfat/article/details/7963608 ...
- 字符编码:ANSI,ASCII,GB2312,GBK,Big5,Unicode和UTF-8
整理自字符编码笔记:ASCII,Unicode和UTF-8 1. ASCII码 我们知道,在计算机内部,所有的信息最终都表示为一个二进制的字符串.每一个二进制位(bit)有0和1两种状态,因此八个二进 ...
- Jar包可执行??
第一次听说,jvm加载包,必须rwx么?
- 【POJ 1639】 Picnic Planning (最小k度限制生成树)
[题意] 有n个巨人要去Park聚会.巨人A和先到巨人B那里去,然后和巨人B一起去Park.B君是个土豪,他家的停车场很大,可以停很多车,但是Park的停车场是比较小.只能停k辆车.现在问你在这个限制 ...
- TDBGrideh表头自动排序设置
自动显示标题行的升降排序标志符(▽降序△升序)并做相应排序DBGridEh组件可以在标题行单元格中显示小三角形升.降排序标志符图片,在运行时可点击标题行,图片自动切换并做相应排序. 具体属性设置如下: ...
- Programming in lua 环境
Lua 用一个名为environment 普通的表来保存所有的全局变量.(更精确的说,Lua在一系列的environment 中保存他的“global”变量,但是我们有时候可以忽略这种多样性)这种结果 ...
- Discuz! x 2.5-3.0 beta 存储型跨站漏洞
漏洞版本: Discuz x 2.5 - 3.0 漏洞描述: Discuz! 已拥有11年以上的应用历史和200多万网站用户案例 是全球成熟度最高.覆盖率最大的论坛软件系统之一,淘帖处发表评论,直接插 ...
- 字符串(AC自动机):HDU 5129 Yong Zheng's Death
Yong Zheng's Death Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 512000/512000 K (Java/O ...