使用情景:比如打電話時(shí)拿起來貼近臉后屏幕會變黑,拿開就變亮.
使用方法:
//1.開啟距離傳感器(注意:默認(rèn)情況距離傳感器是關(guān)閉的)
[UIApplicationsharedApplication].proximitySensingEnabled=YES;
[UIDevicecurrentDevice].proximityMonitoringEnabled=YES;
//2.開啟之后,開始實(shí)時(shí)監(jiān)聽
[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(proximityMonitorDidChange:)name:UIDeviceProximityStateDidChangeNotificationobject:nil];
這時(shí)就可以獲取是否有物體靠近了.
- (void)proximityMonitorDidChange:(NSNotification*)not {
? ?if([UIDevicecurrentDevice].proximityState) {
? ? ? ? NSLog(@"物體靠近");
? ?}else{
? ? ? ? NSLog(@"物體離開");
? ?}
}