距离传感器

距离传感器:默认关闭,打开便可,当你接电话的时候距离传感器会起做用,当你脸靠近屏幕,屏幕灯会熄灭,并自动锁屏,能够防止你的脸误操做,当你脸离开,屏幕灯会自动开启,而且自动解锁web

@implementation ViewControllerspa


- (void)viewDidLoad {.net

    [super viewDidLoad];3d

    

    //1. 打开距离检测orm

    [UIDevice currentDevice].proximityMonitoringEnabled = YES;server

    

    //2. 使用通知来检测距离的变化rem

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceProximityStateDidChangeNotification) name:UIDeviceProximityStateDidChangeNotification object:nil];get

    

}it


#pragma mark 3. 通知绑定的方法io

- (void)deviceProximityStateDidChangeNotification

{

    if ([UIDevice currentDevice].proximityState) {

        //靠近了手机

        NSLog(@"有物体靠近");

    } else {

        NSLog(@"物体走咧");

    }

    

    

}


#pragma mark 移除观察者

- (void)dealloc

{

    [[NSNotificationCenter defaultCenter] removeObserver:self];

}


@end