iOS 探讨之 传感器库(CoreMotion.framework)

来源:互联网 发布:冲田杏梨seo 681 编辑:程序博客网 时间:2024/06/05 20:44
阐述
还是来几句没有涵养的话吧~ 传感器是个好东西,但是太难理解了,太难玩了,找了几篇像样的资料,整合一下,记录之。
探讨
    iPhone 5之后,Apple公司就已经开始为iPhone设备装配上了很多传感器,如:
1. 运动传感器\加速度传感器\加速计(Motion/Accelerometer Sensor)
    感应设备的运动(摇一摇、计步器)

2. 环境光传感器(Ambient Light Sensor)
    感应周边环境光线的强弱(自动调节屏幕亮度)

3. 距离传感器(Proximity Sensor)
    感应是否有其他物体靠近设备屏幕(打电话自动锁屏)

4. 磁力计传感器(Magnetometer Sensor)
    感应周边的磁场(合盖锁屏)

5. 内部温度传感器(Internal Temperature Sensor)
    感应设备内部的温度(提醒用户降温,防止损伤设备)

6. 湿度传感器(Moisture Sensor)
    感应设备是否进水(方便维修人员)

7. 陀螺仪(Gyroscope)
    感应设备的持握方式(赛车类游戏)

    为了推广设备的价值,Apple公司在 iOS 5之后开始为开发者提供传感器对应的库 --- CoreMotion.framework
    作为传感器的封装,其包含了以前所支持的各种传感器,并且在原有的基础上对有关数据进行加工,省去开发者进行有关的数据工作,提出了DeviceMotion概念。

    下面介绍库中所包含的以前的概念
    各个传感器是否可用 ( 可在CMMotionManager.h 查询)
-  加速器
@property(readonly, nonatomic, getter=isAccelerometerAvailable) BOOL accelerometerAvailable __TVOS_PROHIBITED

-  陀螺仪
@property(readonly, nonatomic, getter=isGyroAvailable) BOOL gyroAvailable __TVOS_PROHIBITED;

-  磁力仪
@property(readonly, nonatomic, getter=isMagnetometerAvailable) BOOL magnetometerAvailable NS_AVAILABLE(NA,5_0) __TVOS_PROHIBITED;


    设置各个传感器感应时间间隔
-  加速器
@property(assign, nonatomic) NSTimeInterval accelerometerUpdateInterval __TVOS_PROHIBITED;

-  陀螺仪
@property(assign, nonatomic) NSTimeInterval gyroUpdateInterval __TVOS_PROHIBITED;

-  磁力仪
@property(assign, nonatomic) NSTimeInterval magnetometerUpdateInterval NS_AVAILABLE(NA,5_0) __TVOS_PROHIBITED;


    使用传感器前需要调用库:
   @import CoreMotion;

    声明传感器管理者
    manager = [[CMMotionManager alloc] init];

    最好不要在主线程使用有关功能,否则影响用户体验
    NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];


使用各个传感器
-  加速器




x、y、z 表示iOS设备在X、Y、Z方向上的倾斜状况,返回的数值范围为-1~+1的浮点数
    [manager startAccelerometerUpdatesToQueue:operationQueue withHandler:^(CMAccelerometerData * _Nullable accelerometerData, NSError * _Nullable error) {        NSLog(@"x:%f,y:%f,z:%f",accelerometerData.acceleration.x,accelerometerData.acceleration.y,accelerometerData.acceleration.z);    }];


-  陀螺仪
从陀螺仪中得到的数据为X、Y、Z三个轴的角速度
    [manager startGyroUpdatesToQueue:operationQueue withHandler:^(CMGyroData * _Nullable gyroData, NSError * _Nullable error) {        NSLog(@"x:%f,y:%f,z:%f",gyroData.rotationRate.x,gyroData.rotationRate.y,gyroData.rotationRate.z);    }];


-  磁力仪
从磁力仪中得到的数据为X、Y、Z三个轴的磁感应强度,读出来的数值单位为特斯拉
    [manager startMagnetometerUpdatesToQueue:operationQueue withHandler:^(CMMagnetometerData * _Nullable magnetometerData, NSError * _Nullable error) {        NSLog(@"x:%f,y:%f,z:%f",magnetometerData.magneticField.x,magnetometerData.magneticField.y,magnetometerData.magneticField.z);    }];


关闭有关感应器(在不需要感应器的场景下,为用户省电请关闭有关传感器)
-  加速器
    [manager stopAccelerometerUpdates];

-  陀螺仪
    [manager stopGyroUpdates];

-  磁力仪
    [manager stopMagnetometerUpdates];


    从上述感应器中得到的数据,在非专业人员眼中,非常晦涩难懂,故苹果帮助开发人员封装数据分析过程,提供最终结果 - Device Motion
    在  CMDeviceMotion 类中包含可供使用的属性
设备目前的姿势
@property(readonly, nonatomic) CMAttitude *attitude;

姿势中包含三个重要的数据 Pitch、Roll、Yaw,分别表示设备在X、Y、Z三个轴上的旋转角度
@property(readonly, nonatomic) double pitch;@property(readonly, nonatomic) double roll;@property(readonly, nonatomic) double yaw;


设备目前的重力方向
@property(readonly, nonatomic) CMAcceleration gravity;

用户所给的加速度
@property(readonly, nonatomic) CMAcceleration userAcceleration;

角速度
@property(readonly, nonatomic) CMRotationRate rotationRate;

磁场状况
@property(readonly, nonatomic) CMCalibratedMagneticField magneticField NS_AVAILABLE(NA,5_0);


其的检测时间、是否开启等都类似上传各个传感器的写法
设置检测时间间隔
@property(assign, nonatomic) NSTimeInterval deviceMotionUpdateInterval __TVOS_PROHIBITE

检查是否开启有关设备
@property(readonly, nonatomic, getter=isDeviceMotionAvailable) BOOL deviceMotionAvailable __TVOS_PROHIBITED;

开始使用获取有关数值
[manager startDeviceMotionUpdatesToQueue:operationQueue withHandler:^(CMDeviceMotion * _Nullable motion, NSError * _Nullable error) {       dispatch_async(dispatch_get_main_queue(), ^{                     float pitch = motion.attitude.pitch;           float roll = motion.attitude.roll;           float yaw = motion.attitude.yaw;                     self.displayPitchLabel.text = [NSString stringWithFormat:@"%2.0f",pitch*180/M_PI];                     self.displayRollLabel.text = [NSString stringWithFormat:@"%2.0f",roll*180/M_PI];                     self.displayYawLabel.text = [NSString stringWithFormat:@"%2.0f",yaw*180/M_PI];       });    }];
   
关闭有关设备
    [manager stopDeviceMotionUpdates];


除了上方封装的传感器外,iPhone还包含了距离传感器,如当用户接听电话的时候屏幕变暗 (此传感器必须在iPhone竖屏下使用)
设置距离传感器开启
    [[UIDevice currentDevice] setProximityMonitoringEnabled:YES];

判断距离传感器是否开启
    [UIDevice currentDevice].proximityMonitoringEnabled == YES;

监听距离传感状态
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(proximateSensorChange:) name:UIDeviceProximityStateDidChangeNotification object:nil];

不使用时请移除该监听事件
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceProximityStateDidChangeNotification object:nil];

距离状态 YES 靠近 NO 远离
    [UIDevice currentDevice].proximityState

下面附上设备晃动有关代码:
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {    if (event.subtype == UIEventSubtypeMotionShake) {        NSLog(@"摇晃中 ~ ");    }}- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {    if (event.subtype == UIEventSubtypeMotionShake) {        NSLog(@"摇晃结束 ~ ");    }}- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event {    if (event.subtype == UIEventSubtypeMotionShake) {        NSLog(@"摇晃取消 ~ ");    }}



0 0