判断是不是有物体靠近屏幕
2017-06-13 本文已影响2人
40dd4b561abe
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//1.开启传感器
[UIDevice currentDevice].proximityMonitoringEnabled = YES;
//2.监听设备发出的通知()
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ProximityStateDidChange:) name:UIDeviceProximityStateDidChangeNotification object:nil];
}
//处理通知
-(void)ProximityStateDidChange:(NSNotification *)note
{
NSLog(@"%@",note);
if ([UIDevice currentDevice].proximityState) {
NSLog(@"1");
}else
{
NSLog(@"2");
}
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}