// 添加通知
[[UIDevice currentDevice] setProximityMonitoringEnabled:YES];
if ([UIDevice currentDevice].proximityMonitoringEnabled == YES)
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(proximitySensorChange:) name:UIDeviceProximityStateDidChangeNotification object:nil];
2.实现方法
- (void)proximitySensorChange:(NSNotificationCenter *)notification;
{
if ([[UIDevice currentDevice] proximityState] == YES) {
NSLog(@"Device is close to user");
[[UIScreen mainScreen] setWantsSoftwareDimming:NO];
}else{
NSLog(@"Device is not close to user");
[[UIScreen mainScreen] setWantsSoftwareDimming:YES];
}
}
3.删除脸部距离检测
[[UIDevice currentDevice] setProximityMonitoringEnabled:YES];
if ([UIDevice currentDevice].proximityMonitoringEnabled == YES) {
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceProximityStateDidChangeNotification object:nil];
}
[[UIDevice currentDevice] setProximityMonitoringEnabled:NO];