传感器--------接近传感器

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //开启距离传感器检测
    [UIDevice currentDevice].proximityMonitoringEnabled = true;
    //添加<span style="font-family: Arial, Helvetica, sans-serif;">距离传感器距离</span><span style="font-family: Arial, Helvetica, sans-serif;">改变的通知</span>
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(proximityStateDidChange) name:UIDeviceProximityStateDidChangeNotification object:nil];
}

- (void)proximityStateDidChange {
    
    if ([UIDevice currentDevice].proximityState) {
        NSLog(@"接近");
        //此处写对应功能代码
    } else {
        NSLog(@"远离");
        //此处写对应功能代码
    }
}

- (void)dealloc {
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

你可能感兴趣的:(传感器,距离传感器,接近传感器)