• 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];
    }
  • 相关阅读:
    干货分享|安全测试起航之旅
    干货|宏巍软件之Java线程监控之旅
    大数据时代,业务运维驱动下的企业变革
    什么是业务运维,企业如何实现互联网+业务与IT的融合
    不谈业务运维的IT主管早晚被淘汰 这里是10条干货
    详解APM数据采样与端到端
    CTO对话:云端融合下的移动技术创新
    【1111元天天拿!充一送二玩真哒!】双十一任性活动
    引领手机流量营销 嘿嘿流量打造多场景专业服务
    “烧钱补贴”下的O2O该何去何从?
  • 原文地址:https://www.cnblogs.com/fengmin/p/5505770.html
Copyright © 2020-2023  润新知