下面这段代码可以实现邻近检测功能,当你的身体靠近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

}

}

 

相关文章:

  • 2021-04-04
  • 2021-09-27
  • 2021-10-27
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-20
猜你喜欢
  • 2022-12-23
  • 2021-07-21
  • 2021-10-03
  • 2022-12-23
  • 2021-09-08
  • 2021-11-26
  • 2021-08-25
相关资源
相似解决方案