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

 



posted on 2017-05-17 22:52  守望星空  阅读(1724)  评论(0编辑  收藏  举报

导航