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传感器原始数据的更多相关文章

  1. IOS 特定于设备的开发:Core Motion基础

    Core Motion框架集中了运动数据处理.该框架是在IOS 4 SDK中引入的,用于取代accelerometer加速计访问.它提供了对3个关键的机载传感器的集中式监测.这些传感器有陀螺仪.磁力计 ...

  2. IOS Core Motion、UIAccelerometer(加速计使用)

    加速计 ● 加速计的作用 ● 用于检测设备的运动(比如摇晃) ● 加速计的经典应用场景 ● 摇一摇 ● 计步器 ● 加速计程序的开发 ● 在iOS4以前:使用UIAccelerometer,用法非常简 ...

  3. iOS开发 传感器(加速计、摇一摇、计步器)

    一.传感器 1.什么是传感器传感器是一种感应\检测周围环境的一种装置, 目前已经广泛应用于智能手机上 传感器的作用用于感应\检测设备周边的信息不同类型的传感器, 检测的信息也不一样 iPhone中的下 ...

  4. iOS开发——高级篇——传感器(加速计、摇一摇、计步器)

    一.传感器 1.什么是传感器传感器是一种感应\检测周围环境的一种装置, 目前已经广泛应用于智能手机上 传感器的作用用于感应\检测设备周边的信息不同类型的传感器, 检测的信息也不一样 iPhone中的下 ...

  5. iOS 神秘而又强大的传感器系统 (附demo)

    iOS中的各种传感器: 随着科技的发展,机器感知人的行为!Goole的无人驾驶汽车到李彦宏的无人驾汽车,都带入了各种计算及传感. 为了研究自然现象和制造劳动工具,人类必须了解外界的各类信息.了解外界信 ...

  6. iOS 传感器集锦

    https://www.jianshu.com/p/5fc26af852b6 传感器集锦:指纹识别.运动传感器.加速计.环境光感.距离传感器.磁力计.陀螺仪   效果预览.gif 一.指纹识别 应用: ...

  7. 【读书笔记】iOS-使用传感器

    和其他的高端智能机一样,iPhone携带了很多传感器:照相机,加速度计,GPS模块和数字指南针. 使用Core Motion框架,你的应用可以读取来自于加速度计,磁力计以及陀螺仪的运动数据. 近距离传 ...

  8. Core Services层

    本文转载至 http://jingyan.baidu.com/article/cdddd41c57360853cb00e124.html Core Services层是系统很多部分的基础部分,也许应用 ...

  9. iOS---开发实用传感器

    传感器 1.什么是传感器 传感器是一种感应\检测装置, 目前已经广泛应用于智能手机上 2.传感器的作用 用于感应\检测设备周边的信息 不同类型的传感器, 检测的信息也不一样 iPhone中的下面现象都 ...

随机推荐

  1. [原博客] POJ 1740 A New Stone Game

    题目链接题意:有n堆石子,两人轮流操作,每次每个人可以从一堆中拿走若干个扔掉(必须),并且可以从中拿走一些分到别的有石子的堆里(可选),当一个人不能拿时这个人输.给定状态,问是否先手必胜. 我们参考普 ...

  2. Codeforces Round #197 (Div. 2) : B

    也是水题一个,不过稍微要细心点.... 贴代码: #include<iostream> using namespace std; long long n,m; ; int main() { ...

  3. 李洪强iOS开发Swift篇—03_字符串和数据类型

    李洪强iOS开发Swift篇—03_字符串和数据类型 一.字符串 字符串是String类型的数据,用双引号""包住文字内容  let website = "http:// ...

  4. Android用户界面UI组件--AdapterView及其子类(五) Spinner和SpinnerAdapter

    Spinner就是下拉框组件,可以自定义下拉布局样式,可以使用ArrayAdapter以及SpinnerAdapter适配 在Adapter中实现SpinnerAdapter,继承BaseAdapte ...

  5. svn文件提交时强制写注释

    这个操作需要修改版本库中的一个钩子文件,在你创建的版本库中有一个hooks文件夹,初始的时候其中有一个文件叫pre-commit.tmpl 在windows下将其修改为pre-commit.bat,在 ...

  6. HUD 2089

    不要62 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  7. Oracle创建表空间、创建用户、授权用户、导入dmp备份语句

    create tablespace TOPSCF_CHS_TS datafile 'D:\TableSpace\TOPSCF_CHS_TS.dbf' size 512M  extent managem ...

  8. OSI 七层模型和 TCP/IP 协议比较

      OSI (Open System Interconnection), 开放式系统互联参考模型.从下到上七层模型功能及其代表协议: 物理层(Physical) :规定了激活.维持.关闭通信端点之间的 ...

  9. Mysql导出导入乱码问题解决

    MySQL从4.1版本开始才提出字符集的概念,所以对于MySQL4.0及其以下的版本,他们的字符集都是Latin1的,所以有时候需要对mysql的字符集进行一下转换,MySQL版本的升级.降级,特别是 ...

  10. 388. Longest Absolute File Path

    就是看哪个文件的绝对路径最长,不是看最深,是看最长,跟文件夹名,文件名都有关. \n表示一波,可能存在一个文件,可能只有文件夹,但是我们需要检测. 之后的\t表示层数. 思路是如果当前层数多余已经有的 ...