距離傳感器:默認關閉,打開便可,當你接電話的時候距離傳感器會起做用,當你臉靠近屏幕,屏幕燈會熄滅,並自動鎖屏,能夠防止你的臉誤操做,當你臉離開,屏幕燈會自動開啓,而且自動解鎖web
@implementation ViewControllerspa
- (void)viewDidLoad {.net
[super viewDidLoad];3d
//1. 打開距離檢測orm
[UIDevice currentDevice].proximityMonitoringEnabled = YES;server
//2. 使用通知來檢測距離的變化rem
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceProximityStateDidChangeNotification) name:UIDeviceProximityStateDidChangeNotification object:nil];get
}it
#pragma mark 3. 通知綁定的方法io
- (void)deviceProximityStateDidChangeNotification
{
if ([UIDevice currentDevice].proximityState) {
//靠近了手機
NSLog(@"有物體靠近");
} else {
NSLog(@"物體走咧");
}
}
#pragma mark 移除觀察者
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}