• IOS UIDevice距离传感器(打开 关闭)


    ●  什么是传感器
    ●  传感器是一种感应检测装置, 目前已经广泛应用于智能手机上
    ●  iPhone5中内置的传感器有

     ●  运动传感器加速度传感器加速计(Motion/Accelerometer Sensor)

    ●  环境光传感器(Ambient Light Sensor)

    ●  距离传感器(Proximity Sensor)

    ●  磁力计传感器(Magnetometer Sensor)

    ●  内部温度传感器(Internal Temperature Sensor)

    ●  湿度传感器(Moisture Sensor)

    ●  陀螺仪(Gyroscope)

    传感器总结

     

    距离传感器的使用:
    // 开启距离感应功能
    [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(@"某个物体远离了设备屏幕"); // 屏幕会自动解锁

    } }

    //打开距离感应功能

    #import "ViewController.h"
    
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        
        // 1.开启距离传感器(注意: 默认情况距离传感器是关闭的)
    //    [UIApplication sharedApplication].proximitySensingEnabled = YES;
        // 只要开启之后, 就开始实时监听
        [UIDevice currentDevice].proximityMonitoringEnabled = YES;
        
        // 2.当监听到有物体靠近设备时系统会发出通知
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(proximityStateDidChange:) name:UIDeviceProximityStateDidChangeNotification object:nil];
        
    }
    - (void)dealloc
    {
        [[NSNotificationCenter defaultCenter] removeObserver:self];
    }
    
    // 当监听到有物体靠近设备时调用
    - (void)proximityStateDidChange:(NSNotification *)note
    {
    //    NSLog(@"%@", note);
       if( [UIDevice currentDevice].proximityState)
       {
           NSLog(@"有物体靠近");
       }else
       {
           NSLog(@"物体离开");
       }
    }
    
    
    
    
    @end
    View Code



  • 相关阅读:
    C语言I博客作业02
    第一次C语言作业
    C语言I博客作业02
    网页常用分享代码
    js生成验证码并验证
    js时间格式的转换
    Git 常用命令
    ASP.NET MVC中使用事务写法
    数据库游标导入数据
    js截取所需字符串长度
  • 原文地址:https://www.cnblogs.com/liuwj/p/6869983.html
Copyright © 2020-2023  润新知