您的位置:首页 > 其它

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

2016-04-06 12:33 417 查看
#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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息