#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传感器--------接近传感器
最新推荐文章于 2024-07-29 18:04:30 发布
本文介绍了一个简单的iOS应用程序示例,该程序通过Objective-C实现了设备距离传感器的功能。文章展示了如何在ViewController类中设置并监听距离传感器状态的变化,并根据设备是否靠近用户来触发不同的应用行为。
386

被折叠的 条评论
为什么被折叠?



