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中的下面现象都 ...
随机推荐
- 得到bundle seed id
- (NSString *)bundleSeedID { NSDictionary *query = [NSDictionary dictionaryWithObjectsAndKeys: (__br ...
- http://jinnianshilongnian.iteye.com/blog/1996071
http://jinnianshilongnian.iteye.com/blog/1996071 http://my.oschina.net/jkcui/blog/388400 http://tian ...
- FFMPEG-数据结构解释(AVCodecContext,AVStream,AVFormatContext)
http://blog.csdn.net/yuan892173701/article/details/8702333 AVCodecContext 这是一个描述编解码器上下文的数据结构,包含了众多编 ...
- 我新买的红米手机,新浪和360浏览器都能进,也能看电视,就是不能上手机QQ和微信
1.请您在桌面下.点击手菜单键-全局搜索,输入网络助手,点击流量排行,点击批量联网控制,查看该软件下(不能上网的应用)wifi和流量2G/3G下方的选项是否都勾选的.如果没有勾选,请您勾选. 2:仍然 ...
- TF卡的Class4/Class6/Class10
TF卡全称TransFlash卡,即T-Flash又称MicroSD,随设备微型化的需要,TF卡用途越来越广,速度也越来越快,从最初的Class2早已发展到目前主流的Class10,TF卡体积为15m ...
- 翻译文章“AST 模块:用 Python 修改 Python 代码”---!!注意ironpathyon未实现此功能
https://github.com/upsuper/blog/commit/0214fdd084c4adf2de2ed9912d644fb59ce13a1c +Title: [翻译] AST 模块: ...
- show processlist 输出ID 和 information_schema.PROCESSLIST 的id,information_schema.innodb_trx的TRX_MYSQL_T
Session 1: mysql> start transaction; Query OK, 0 rows affected (0.00 sec) mysql> update Client ...
- Android中的常见时区
方法: private void printTimeZone(){ String[] ids= TimeZone.getAvailableIDs(); for (int i = 0; i < i ...
- WordPress Woopra plugin remote PHP arbitrary code execution exploit.
测试方法: 提供程序(方法)可能带有攻击性,仅供安全研究与教学之用,风险自负! # Exploit Title: woopra plugins execute arbitrary PHP code E ...
- 推荐一个Xcode插件: KSImageNamed (自动补全图片文件名称, 并显示图片大小)
http://www.csdn.net/article/2014-05-04/2819586-the-best-xcode-plugins 5. KSImageNamed KSImageNamed是一 ...