- (void)ViewDidLoad { // 打开距离传感器,开启距离检测功能 [UIDevice currentDevice].proximityMonitoringEnabled = YES; // 监听距离改变 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(proximityStateDidChange:) name:UIDeviceProximityStateDidChangeNotification object:nil]; } - (void)proximityStateDidChange:(NSNotification *)note { if ([UIDevice currentDevice].proximityState) { NSLog(@"有物体靠近设备屏幕"); } else { NSLog(@"有物体远离设备屏幕"); } }
时间: 2024-10-10 08:19:00