随着iOS设备的使用者增多,产品的体验越来越高,就增加了距离传感器功能。
这个功能实现比较简单,只需实现如下代码即可:
- (void)viewDidLoad {
// 开启距离感应功能
[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(@"某个物体远离了设备屏幕"); // 屏幕会自动解锁
}
}