博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Core Motion传感器原始数据
阅读量:5892 次
发布时间:2019-06-19

本文共 7576 字,大约阅读时间需要 25 分钟。

1、访问原始的Motion数据 #import 
#import
@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 
#import
@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];}

 

转载地址:http://znfsx.baihongyu.com/

你可能感兴趣的文章
一个IO的传奇一生(2)
查看>>
linux文件描述符
查看>>
C++ const 详解
查看>>
给Github上的项目添加开源协议
查看>>
imx53 start board 开箱照
查看>>
免费的编程中文书籍索引
查看>>
struts2引入标签时遇到的问题
查看>>
Hibernate例子-自己写的一个干净的给予注解的Hibernate例子
查看>>
WorkFlow入门Step.6—Building a Console Application -For-WF4.0
查看>>
sota系统服务进程的启动与停止(章节:4.2)
查看>>
精选9个值得学习的 HTML5 效果【附源码】
查看>>
查看Beyond Compare比较图片实际尺寸的方法
查看>>
Tomcat 安全配置与性能优化
查看>>
线性表顺序存储-练习题1
查看>>
jQuery中尺寸
查看>>
Python学习day1作业总结
查看>>
两种方法实现10进制和62进制互转
查看>>
无难度教你学会Yii2的资源管理(Asset Manager) - 发布资源
查看>>
Android MVP 模式 项目初体验(一)
查看>>
Android四大组件:BroadcastReceiver史上最全面解析
查看>>