IOS 传感器

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/WUWUWEIWEILONGLONG/article/details/74315880

一 传感器

  • 什么是传感器 
    • 传感器是一种感应\检测周围环境的一种装置
  • 传感器的作用

    • 用于感应\检测设备周边的信息
    • 不同类型的传感器, 检测的信息也不一样
  • iPhone内置的传感器有

    • 运动传感器\加速度传感器\加速计(Motion/Accelerometer Sensor)
    • 环境光传感器(Ambient Light Sensor)
    • 距离传感器(Proximity Sensor)
    • 磁力计传感器(Magnetometer Sensor)
    • 内部温度传感器(Internal Temperature Sensor)
    • 湿度传感器(Moisture Sensor)
    • 陀螺仪(Gyroscope) 
      … …

二 距离传感器

  • 默认情况下,每一个应用程序距离传感器都是关闭状态 
    • 如果需要,需要通过代码将其打开
// 过期代码  [UIApplication sharedApplication].proximitySensingEnabled

[UIDevice currentDevice].proximityMonitoringEnabled = YES;
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

三 加速计信息获取##

  • UIAccelerometer方法,该方法已过期
- (void)viewDidLoad {
    [super viewDidLoad];

    // 1.获取单例对象
    UIAccelerometer *acceleromter = [UIAccelerometer sharedAccelerometer];
    // 2.设置代理
    acceleromter.delegate = self;
    // 3.设置采样间隔
    acceleromter.updateInterval = 1.0 / 5;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
#pragma mark - 实现UIAccelerometer的代理方法
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
    NSLog(@"x:%f y:%f z:%f", acceleration.x, acceleration.y, acceleration.z);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

四 CoreMotion方法##

  • Core Motion获取数据的两种方式

    • push:实时采集所有数据(采集频率高)
    • pull:在有需要的时候,再主动去采集数据
  • 加速计信息获取(pull/push)

 // 1.创建运动管理者对象
 CMMotionManager *mgr = [[CMMotionManager alloc] init];

  // 2.判断加速计是否可用
  if (!self.mgr.isAccelerometerAvailable) {
        NSLog(@"加速计不可用,请更换手机");
        return;
  } 
  // 3.设置采样间隔
  self.mgr.accelerometerUpdateInterval = 1.0;

  // 4.开始采样
  [self.mgr startAccelerometerUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMAccelerometerData * _Nullable accelerometerData, NSError * _Nullable error) {
        if (error) {
            NSLog(@"%@", error);
            return;
        }
   // 获取加速计的值
   CMAcceleration acceleration = accelerometerData.acceleration;
   NSLog(@"x:%f y:%f z:%f", acceleration.x, acceleration.y, acceleration.z);
    }];
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
// 开始采样
[self.mgr startAccelerometerUpdates];
  • 1
  • 2
  • 1
  • 2
  • 陀螺仪信息获取(pull/push)
 // push方式获取陀螺仪信息
 // 1.判断陀螺仪是否可用
 if (!self.mgr.isGyroAvailable) {
        NSLog(@"设备小于iPhone4,或者陀螺仪损坏");
        return;
    }
 // 2.设置采样
 self.mgr.gyroUpdateInterval = 1.0 / 10;   
 // 3.开始采样
    [self.mgr startGyroUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMGyroData * _Nullable gyroData, NSError * _Nullable error) {
        if (error) {
            NSLog(@"%@", error);
            return;
        } 
  // 获取陀螺仪的信息
  CMRotationRate rotationRate = gyroData.rotationRate;
  NSLog(@"x:%f y:%f z:%f", rotationRate.x, rotationRate.y, rotationRate.z);
    }];
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
// 开始采样
[self.mgr startGyroUpdates];
  • 1
  • 2
  • 1
  • 2

五 摇⼀摇功能##

  • 监控摇一摇的方法 
    • 方法1:通过分析加速计数据来判断是否进行了摇一摇操作(比较复杂)
    • 方法2:iOS自带的Shake监控API(非常简单)
  • 判断摇一摇的步骤:实现3个摇一摇监听方法
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event /** 检测到摇动 */

- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event /** 摇动取消(被中断) */

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event /** 摇动结束 */
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

六 计步器##

  • CMStepCounter方法,已过期
// 1.判断计步器是否可用
    if (![CMStepCounter isStepCountingAvailable]) {
        NSLog(@"计步器不可用");
        return;
    }
// 2.开始计步
  // 2.1.创建计步器
  CMStepCounter *stepCounter = [[CMStepCounter alloc] init];

  // 2.2.开始计步
  // updateOn : 用户走了多少步之后,更新block
  NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    [stepCounter startStepCountingUpdatesToQueue:queue updateOn:5 withHandler:^(NSInteger numberOfSteps, NSDate * _Nonnull timestamp, NSError * _Nullable error) {
        if (error) return;

NSString *stepString = [NSString stringWithFormat:@"您一共走了%ld步", numberOfSteps];
[self.stepLabel performSelectorOnMainThread:@selector(setText:) withObject:stepString waitUntilDone:YES];
    }];
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 新方法CMPedometer
// 1.判断计步器是否可用
    if (![CMPedometer isStepCountingAvailable]) {
        return;
    }
// 2.开始计步
  // 2.1.创建计步对象
  CMPedometer *pedometer = [[CMPedometer alloc] init];

  // 2.2.开始计步
  // FromDate : 从什么时间开始计步
  NSDate *date = [NSDate date];
  [self.pedometer startPedometerUpdatesFromDate:date withHandler:^(CMPedometerData * _Nullable pedometerData, NSError * _Nullable error) {
        if (error) {
            NSLog(@"%@", error);
            return;
        }
        NSLog(@"您一共走了%@步", pedometerData.numberOfSteps);
    }];
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 案例:计算7天一共走了多少步
 NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
 fmt.dateFormat = @"yyyy-MM-dd";
 NSDate *fromDate = [fmt dateFromString:@"2015-9-26"];
 NSDate *toDate = [fmt dateFromString:@"2015-9-28"];

 [self.pedometer queryPedometerDataFromDate:fromDate toDate:toDate withHandler:^(CMPedometerData * _Nullable pedometerData, NSError * _Nullable error) {
     NSLog(@"%@", pedometerData.numberOfSteps);
    }];

猜你喜欢

转载自blog.csdn.net/WUWUWEIWEILONGLONG/article/details/74315880