iPhone开发:proximityMonitoring邻近检测

下面这段代码可以实现邻近检测功能,当你的身体靠近iPhone而不是触摸的时候,iPhone将会做出反应。(需要一定的面的影射,约5mm左右的时候就会触发)

在程序初始化处,添加一个监听

UIDevice *device = [UIDevice currentDevice];

device.proximityMonitoringEnabled = YES;

if (device.proximityMonitoringEnabled == YES)

[[NSNotificationCenter defaultCenter] addObserver:self 

selector:@selector(proximityChanged:) 

name:UIDeviceProximityStateDidChangeNotification object:device];

消息响应

- (void) proximityChanged:(NSNotification *)notification {

    UIDevice *device = [notification object];

    NSLog(@"In proximity: %i", device.proximityState);

if(device.proximityState==1){

//do something

}

}

原文地址:https://www.cnblogs.com/javawebsoa/p/2458492.html