zoukankan      html  css  js  c++  java
  • 传感器+加速计+摇一摇

    概述

    sensor

    iPhone自带很多传感器,常见的如下表所示:

    系统针对各种sensor都有相应的使用方法,eg: 距离传感器使用

    // 开启距离感应功能
    [UIDevice currentDevice].proximityMonitoringEnabled = YES;
    // 监听距离感应的通知
    [[NSNotificationCenter defaultCenter] addObserver:self
    selector:@selector(proximityChange:)
    name:UIDeviceProximityStateDidChangeNotification
    object:nil];
    
    - (void)proximityChange:(NSNotificationCenter *)notification {
        if ([UIDevice currentDevice].proximityState == YES) {
        NSLog(@"某个物体靠近了设备屏幕"); // 屏幕会自动锁住
        } else {
        NSLog(@"某个物体远离了设备屏幕"); // 屏幕会自动解锁
        }
    }

    更多设备信息

    设备型号、CPU情况、内存使用情况、硬盘使用情况 ...
    是否越狱、装了哪些传感器、当前运行的进程 ...

    有2种方法获取更多的设备信息
      导入底层的C语言库,通过底层的C语言函数获取(较复杂,需要很多时间去研究)
      使用第三方库(用OC封装了底层的C函数)
        https://github.com/Shmoopi/iOS-System-Services
        https://github.com/erica/uidevice-extension

    加速计

    》加速计可以用来检测设备的运动,摇一摇/计步器等常见应用都是基于加速计实现的。

    》加速计主要通过:检测设备在X、Y、Z轴上的加速度 (哪个方向有力的作用,哪个方向运动了),根据加速度数值,就可以判断出在各个方向上的作用力度

    》加速计的开发主要有两种方式:

      使用UIAccelerometer,用法非常简单(到了iOS5就已经过期)
      从iOS4开始:CoreMotion.framework

    UIAccelerometer开发步骤

    1、获取单例对象

    UIAccelerometer *accelerometer = [UIAccelerometer sharedAccelerometer];

    2、设置代理

    accelerometer.delegate = self;

    3、设置采样间隔

    accelerometer.updateInterval = 1.0/30.0; // 1秒钟采样30次

    4、实现代理方法

    - (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
    // acceleration中的x、y、z三个属性分别代表每个轴上的加速度

    CoreMotion(Core Service Layer)

    》iOS4中对加速计进行了全面升级,引入陀螺仪;增加了专门处理motion的框架CoreMotion.framework

    Core Motion不仅能够提供实时的加速度值和旋转速度值,更重要的是,苹果在其中集成了很多牛逼的算法
    》CoreMotion获取数据的两种方式

      push:实时采集所有数据(采集频率高)-采集方式跟UIAccelerometer一样

      pull:在有需要的时候,再主动去采集数据

    》push的开发步骤

       // 1.创建coreMotion管理者
        //    CMMotionManager *mgr = [[CMMotionManager alloc] init];
        self.mgr = [[CMMotionManager alloc] init];
        
        // 2.判断加速计是否可用
        if (self.mgr.isAccelerometerAvailable) {
            /*
             isAccelerometerActive 是否正在采集
             accelerometerData 采集到得数据
             startAccelerometerUpdates  pull
             startAccelerometerUpdatesToQueue  push
             stopAccelerometerUpdates 停止采集
             accelerometerUpdateInterval 采样时间
             */
            // 3.设置采样时间
            self.mgr.accelerometerUpdateInterval = 1 / 30.0;
            // 4.开始采样
            
            [self.mgr startAccelerometerUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) {
                // 这个block是采集到数据时就会调用
                if (error) return ;
                CMAcceleration acceleration = accelerometerData.acceleration;
                NSLog(@"x = %f y = %f z = %f", acceleration.x, acceleration.y , acceleration.z);
            }];
        }else
        {
            NSLog(@"加速计不可用");
        }

    》pull的开发步骤

    //创建运动管理者对象
    CMMotionManager *mgr = [[CMMotionManager alloc] init];
    
    //判断加速计是否可用
    if (mgr.isAccelerometerAvailable) { // 加速计可用 }
    
    //开始采样
    - (void)startAccelerometerUpdates;
    
    //在需要的时候采集加速度数据
    CMAcceleration acc = mgr.accelerometerData.acceleration;
    NSLog(@"%f, %f, %f", acc.x, acc.y, acc.z);

     摇一摇

    监控摇一摇的方法
    方法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 /** 摇动结束 */
  • 相关阅读:
    音乐情感识别
    SoftmaxWithLoss函数和师兄给的loss有哪些区别呢
    内积层和全连接层是一样的
    caffe中的Local Response Normalization (LRN)有什么用,和激活函数区别
    caffe官网的部分翻译及NG的教程
    couldn't import dot_parser
    apt-get -f install
    Spring常用注解总结 hibernate注解
    Set Map List Iterator
    iframe 与frameset
  • 原文地址:https://www.cnblogs.com/shanhua/p/4781501.html
Copyright © 2011-2022 走看看