使用情景:比如打电话时拿起来贴近脸后屏幕会变黑,拿开就变亮.
使用方法:
//1.开启距离传感器(注意:默认情况距离传感器是关闭的)
[UIApplicationsharedApplication].proximitySensingEnabled=YES;
[UIDevicecurrentDevice].proximityMonitoringEnabled=YES;
//2.开启之后,开始实时监听
[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(proximityMonitorDidChange:)name:UIDeviceProximityStateDidChangeNotificationobject:nil];
这时就可以获取是否有物体靠近了.
- (void)proximityMonitorDidChange:(NSNotification*)not {
if([UIDevicecurrentDevice].proximityState) {
NSLog(@"物体靠近");
}else{
NSLog(@"物体离开");
}
}