iOS传感器篇:CoreMotion使用

iOS传感器篇:CoreMotion初探


使用

CMAltimeter

气压计


#import 

 //设备是否支持气压计

    NSLog(@"%d",[CMAltimeter isRelativeAltitudeAvailable]);

    //启用气压计

    self.altimeter = [[CMAltimeter alloc]init];

    [self.altimeter startRelativeAltitudeUpdatesToQueue:NSOperationQueue.mainQueue withHandler:^(CMAltitudeData * _Nullable altitudeData, NSError * _Nullable error) {

        NSLog(@"%lf",[altitudeData.relativeAltitude floatValue]);

        NSLog(@"%@",error);

    }];

导入


import 


@property(strong,nonatomic)CMMotionManager *Manager;

使用

1. 创建CMMotionManager 对象

2. 为CMMotionManager设置获取加速度数据,螺旋仪数据,磁场数据频率,通常设置xxxUpdateInterval属性,单位时间为秒

//      XXX 为:accelerometer(加速度)

//             gyro(螺旋仪)

//             magnetometer(磁场)

3. 调用CMMotionManger startXXXUpdatesToQueue:queue withHanlder:方法来周期性地获取数据,

4. 如果出现错误,想要中止获取这些数据 调用stopXXXUpdates 方法停止


    self.Manager = [[CMMotionManager alloc]init];

    NSOperationQueue *queque = [[NSOperationQueue alloc]init];

    if (self.Manager.accelerometerActive) {

        //设置CMMotionManager 的加速度更新频率为0.1s

        self.Manager.accelerometerUpdateInterval = 0.1;

        //使用代码块开始获取加速度数据

        [self.Manager startAccelerometerUpdatesToQueue:queque withHandler:^(CMAccelerometerData * _Nullable accelerometerData, NSError * _Nullable error) {

            if(error){

                [self.Manager stopAccelerometerUpdates];

                NSLog(@"获取加速度数据错误%@",error);

            }else{

                    //分别获取系统x y z 加速度数据

                NSLog(@"x-----------%lf",accelerometerData.acceleration.x);

                NSLog(@"y-----------%lf",accelerometerData.acceleration.y);

                NSLog(@"x-----------%lf",accelerometerData.acceleration.z);

            }

        }];

    }else{

        NSLog(@"你的设备不支持加速度数据");

    }

获取螺旋仪数据


    if (self.Manager.gyroActive) {

        //设置螺旋仪数据更新频率

        self.Manager.gyroUpdateInterval = 0.1;

        [self.Manager startGyroUpdatesToQueue:queque withHandler:^(CMGyroData * _Nullable gyroData, NSError * _Nullable error) {

            if (error) {

                [self.Manager stopGyroUpdates];

            }else{

                NSLog(@"x-----------%lf",gyroData.rotationRate.x);

                NSLog(@"y-----------%lf",gyroData.rotationRate.y);

                NSLog(@"z-----------%lf",gyroData.rotationRate.z);

            }

        }];

    }else{

        NSLog(@"你的设备不支持螺旋仪数据");

    }

获取磁场数据


    if (self.Manager.magnetometerActive) {

        //设置磁场更新频率

        self.Manager.magnetometerUpdateInterval = 0.1;

        [self.Manager startMagnetometerUpdatesToQueue:queque withHandler:^(CMMagnetometerData * _Nullable magnetometerData, NSError * _Nullable error) {

            if (error) {

                [self.Manager stopMagnetometerUpdates];

            }else{

                NSLog(@"x-----------%lf",magnetometerData.magneticField.x);

                NSLog(@"y-----------%lf",magnetometerData.magneticField.y);

                NSLog(@"z-----------%lf",magnetometerData.magneticField.z);

            }

        }];

    }else{

        NSLog(@"你的设备不支持磁场数据");

    }

主动请求获取加速度数据,陀螺仪数据,磁场数据

主动请求加速度数据


    if (self.Manager.accelerometerAvailable) {

        CMAccelerometerData *AccelerometerData = self.Manager.accelerometerData;

        NSLog(@"x-----------%lf",AccelerometerData.acceleration.x);

    }

主动请求陀螺仪数据


    if (self.Manager.gyroAvailable) {

        CMGyroData *gyroData = self.Manager.gyroData;

        NSLog(@"x-----------%lf",gyroData.rotationRate.x);

    }

主动请求磁场数据


    if (self.Manager.magnetometerAvailable) {

        CMMagnetometerData *magnetometerData = self.Manager.magnetometerData;

        NSLog(@"x-----------%lf",magnetometerData.magneticField.x);

    }

感知设备移动


    // 如果可以获取设备的动作信息

    if (self.Manager.deviceMotionAvailable) {

        //开始更新设备的动作信息

        [self.Manager startDeviceMotionUpdates];

    }else{

        NSLog(@"该设备deviceMotion不可用");

    }

     NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(Update) userInfo:nil repeats:YES];

}

-(void)Update{

    if (self.Manager.deviceMotionAvailable) {

        CMDeviceMotion *deviceMotion  = self.Manager.deviceMotion;

        NSLog(@"手机顶部转过的夹角(Z轴倾斜):%lf",deviceMotion.attitude.yaw);

        NSLog(@"手机顶部或尾部翘起角度(X轴倾斜):%lf",deviceMotion.attitude.pitch);

        NSLog(@"手机左右侧翘起角度:(Y轴倾斜)%lf",deviceMotion.attitude.roll);

        //陀螺仪信息

        NSLog(@"%lf",deviceMotion.rotationRate.x);

        NSLog(@"%lf",deviceMotion.rotationRate.y);

        NSLog(@"%lf",deviceMotion.rotationRate.z);

        //重力加速度

        NSLog(@"%lf",deviceMotion.gravity.x);

        NSLog(@"%lf",deviceMotion.gravity.y);

        NSLog(@"%lf",deviceMotion.gravity.z);

        //磁场信息(精度)

        NSLog(@"%d",deviceMotion.magneticField.accuracy);

        //xyz磁场强度

        NSLog(@"%lf",deviceMotion.magneticField.field.x);

        NSLog(@"%lf",deviceMotion.magneticField.field.y);

        NSLog(@"%lf",deviceMotion.magneticField.field.z);

    }

计步器


@property(nonatomic,strong)CMStepCounter *stepCounter;

    //计步器

    if ([CMStepCounter isStepCountingAvailable]) {

        self.stepCounter = [[CMStepCounter alloc]init];

        NSOperationQueue *queue1 = [[NSOperationQueue alloc]init];

        //updateOn :每5步执行一次代码块

        [self.stepCounter startStepCountingUpdatesToQueue:queue1 updateOn:5 withHandler:^(NSInteger numberOfSteps, NSDate * _Nonnull timestamp, NSError * _Nullable error) {

            NSLog(@"行走了%ld",numberOfSteps);

        }];

    }else{

        NSLog(@"计步器不可用");

    }

设备状态


#import 


//    CMMotionActivityManager

    //手机,存储用户的存储运动数据,反应步行,跑步,驾驶车辆或处于静止状态

    NSOperationQueue *queue2= [[NSOperationQueue alloc]init];

    [self.motionActivityManager startActivityUpdatesToQueue:queue2 withHandler:^(CMMotionActivity * _Nullable activity) {

        NSLog(@"走路:%d",activity.walking);

        NSLog(@"跑步:%d",activity.running);

        NSLog(@"驾车:%d",activity.automotive);

        NSLog(@"静止:%d",activity.stationary);

        NSLog(@"未知:%d",activity.unknown);

    }];

你可能感兴趣的:(iOS传感器篇:CoreMotion使用)