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中的下面现象都 ...
随机推荐
- Hdu 2979 Expensive Drink
Description There are some water, milk and wine in your kitchen. Your naughty little sister made som ...
- 快速搭建PHP开发环境(PhpStorm+EasyPHP)
写在开头,何为PHP(拍黄片)? P HP是一种开源的通用计算机脚本语言,尤其适用于网络开发并可嵌入HTML中使用(维基百科). 从上我们得出,何为PHP? 1.开源脚本语言. 2.用于网络开发可嵌入 ...
- 安卓开发中,什么样的功能适合抽取成 Library?
我们都知道如果将所有的功能都写成 Library,那么我们在编写应用程序的时候就可以快速便捷的写出想要的功能,因为这些已经事先都实现过了,这样在写代码的时候就可以迅速的将 Library 依赖到我们的 ...
- WEB 开发异常:java.lang.ClassNotFoundException
某个类明明是有的,可是eclipse 启动tomcat服务器运行web项目,出现如题异常. java.lang.ClassNotFoundException 信息: Set web app root ...
- 方便的Chrome取色插件ColorPick Eyedropper [设计, FE必备]
最近在和Design合作开发, 她发过来的原型图有各种各样色配色, 不想让她一个一个地标记颜色, 嫌效率低. 于是自己找到一款方便的Chrome取色插件, 叫做ColorPick Eyedropper ...
- Javascript水平提升
1,学习js分几个阶段,没入门,入门初学者,中级水平,高级水平,ppt水平. 2,没入门的如何学习? 我当初是先学jquery,有css和html基础,有css基础看jq的语法很简单,就是选择符,jq ...
- POJ_3579_Median_(二分,查找第k大的值)
描述 http://poj.org/problem?id=3579 给你一串数,共C(n,2)个差值(绝对值),求差值从大到小排序的中值,偶数向下取. Median Time Limit: 1000M ...
- clang failed with exit code 1 的常见情况
1:文件重复,如生成了一份 xxx副本.m 2:reachablity.h 这个文件经常重复. 以上优先检查 .
- 【转】 viewpage禁止滑动--android
原文网址:http://blog.csdn.net/weiyage/article/details/8175108 最近写一个项目,涉及到viewpager,而变态的客户要求不滑动. 方法很简单 重写 ...
- 【转】Fragment和Activity
原文网址:http://www.cnblogs.com/mengdd/archive/2013/01/11/2856374.html Fragment和Activity的交互 一个Fragment的实 ...