iOS 摇一摇,陀螺仪,距离传感器,计步器简单介绍

来源:互联网 发布:修改ssh端口号 编辑:程序博客网 时间:2024/06/06 01:56

摇一摇,陀螺仪,距离传感器,计步器都是在系统的CoreMotion/CoreMotion.h框架中的,
1、传感器
距离传感器只有真机运行的时候猜可以获取信息
距离传感器设置默认是关闭的,需要手动打开,然后监听通知,

/*        1. 距离传感器(真机) <CoreMotion/CoreMotion.h>     */    // 距离传感器设置打开    [UIDevice currentDevice].proximityMonitoringEnabled = YES;    // 一旦有物体靠近或者离开的时候,就回发送通知    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(proximityStateDidChange) name:UIDeviceProximityStateDidChangeNotification object:nil];

可以根据设备的proximityState属性判断是否有物体接近或离开,如果没有物体接近就永远返回 NO(有物体接近的时候才返回yes ,其他的都是NO),

#pragma mark - 有物体靠近或者离开的时候,就会发送通知- (void)proximityStateDidChange{    if ([UIDevice currentDevice].proximityState) {        NSLog(@"有物品靠近");    } else {        NSLog(@"有物品离开");    }}

2、加速计(真机)(摇一摇)
加速计可以有2种方法,一个是调用系统的房,一个是实现代理,
首先设置管理者对象:

@property (nonatomic, strong) CMMotionManager *mgr; // 管理者对象#pragma mark - 懒加载- (CMMotionManager *)mgr{    if (_mgr == nil) {        _mgr = [[CMMotionManager alloc] init];    }    return _mgr;}

(1)代理方式:遵守代理,设置代理,

<UIAccelerometerDelegate> // 2.加速计代理// 设置代理UIAccelerometer *accelerometer = [UIAccelerometer sharedAccelerometer];    accelerometer.delegate = self; // 设置代理    accelerometer.updateInterval = 1 / 30.0; // 设置采样间隔

实现代理方法,

#pragma mark - 获取到加速计信息的时候回调用此方法// acceleration:里面有x,y,z轴上的加速度- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration{    NSLog(@"x=%f,y=%f,z=%f",acceleration.x,acceleration.y,acceleration.z); // 可用于摇一摇计算物体的位置}

(2)block方法回调,

#pragma mark - 获取加速计信息- (void)pushAccelerometer{    // 判断加速计是否可用    if (!self.mgr.isAccelerometerAvailable) {        return;    }    self.mgr.accelerometerUpdateInterval = 1 / 30.0; // 设置采样间隔    // 开始采样    [self.mgr startAccelerometerUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMAccelerometerData * _Nullable accelerometerData, NSError * _Nullable error) {        if (error) {            return ;        }        CMAcceleration acceleration = accelerometerData.acceleration;        NSLog(@"x=%f,y=%f,z=%f",acceleration.x,acceleration.y,acceleration.z); // 可用于摇一摇计算物体的位置    }];}

3、陀螺仪

#pragma mark - 获取陀螺仪信息- (void)pushGyro{    if (!self.mgr.isGyroAvailable) {        return;    }    self.mgr.gyroUpdateInterval = 1 / 30.0; // 设置采样间隔    [self.mgr startGyroUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMGyroData * _Nullable gyroData, NSError * _Nullable error) {        CMRotationRate rotationRate = gyroData.rotationRate;        NSLog(@"x=%f,y=%f,z=%f",rotationRate.x,rotationRate.y,rotationRate.z);    }];}

4、 计步器,需要真机运行

 /*        5. 计步器(真机) <CoreMotion/CoreMotion.h>     */    // 判断计步器是否可用    if (![CMStepCounter isStepCountingAvailable]) {        return;    }    // 创建计步器对象    CMStepCounter *stepCounter = [[CMStepCounter alloc] init];    // updateOn:走多少步之后调用一次    [stepCounter startStepCountingUpdatesToQueue:[NSOperationQueue mainQueue] updateOn:5 withHandler:^(NSInteger numberOfSteps, NSDate * _Nonnull timestamp, NSError * _Nullable error) {        if (error) {            return ;        }        NSLog(@"%ld",numberOfSteps);    }];

5、摇一摇代理

#pragma mark - 摇一摇代理方法(开始摇)- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event{}#pragma mark - 摇一摇代理方法(取消摇)- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event{}#pragma mark - 摇一摇代理方法(结束摇)- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event{

6、以上就是简单的介绍一下他们的使用,具体的使用可能很复杂,需要在头文件找方法,自己测试一下。

0 0