- 什么是传感器
- 传感器是一种感应检测装置, 目前已经广泛应用于智能手机上
- 传感器的作用
- 用于感应检测设备周边的信息
- 不同类型的传感器, 检测的信息也不一样
- iPhone中的下面现象都是由传感器完成的
- 在地图应用中, 能判断出手机头面向的方向
- 一关灯, iPhone会自动降低亮度让屏幕显得不是那么刺眼
- 打电话时, 人脸贴近iPhone屏幕时, 屏幕会自动锁屏, 达到省电的目的
- ... ...
- Phone5中内置的传感器有
- 运动传感器加速度传感器加速计(Motion/Accelerometer Sensor)
- 环境光传感器(Ambient Light Sensor)
- 距离传感器(Proximity Sensor)
- 磁力计传感器(Magnetometer Sensor)
- 内部温度传感器(Internal Temperature Sensor)
- 湿度传感器(Moisture Sensor)
- 陀螺仪(Gyroscope)
- ... ...
距离传感器的使用
// 开启距离感应功能
[UIDevice currentDevice].proximityMonitoringEnabled = YES;
// 监听距离感应的通知
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(proximityChange:)
name:UIDeviceProximityStateDidChangeNotification
object:nil];
- (void)proximityChange:(NSNotificationCenter *)notification {
if ([UIDevice currentDevice].proximityState == YES) {
NSLog(@"某个物体靠近了设备屏幕"); // 屏幕会自动锁住
} else {
NSLog(@"某个物体远离了设备屏幕"); // 屏幕会自动解锁
}
}