iOS---开发实用传感器
传感器
1.什么是传感器
- 传感器是一种感应\检测装置, 目前已经广泛应用于智能手机上
2.传感器的作用
- 用于感应\检测设备周边的信息
- 不同类型的传感器, 检测的信息也不一样
iPhone中的下面现象都是由传感器完成的
- 在地图应用中, 能判断出手机头面向的方向
- 一关灯, iPhone会自动降低亮度让屏幕显得不是那么刺眼
- 打电话时, 人脸贴近iPhone屏幕时, 屏幕会自动锁屏, 达到省电的目的
3.传感器的类型
- iPhone5中内置的传感器有
- 运动传感器\加速度传感器\加速计(Motion/Accelerometer Sensor)
- 环境光传感器(Ambient Light Sensor)
- 距离传感器(Proximity Sensor)
- 磁力计传感器(Magnetometer Sensor)
- 内部温度传感器(Internal Temperature Sensor)
- 湿度传感器(Moisture Sensor)
- 陀螺仪(Gyroscope)
传感器类型
传感器类型 | 作用 |
---|---|
环境光传感器 | 感应周边环境光线的强弱(自动调节屏幕亮度) |
距离传感器 | 感应是否有其他物体靠近设备屏幕(打电话自动锁屏) |
磁力计传感器 | 感应周边的磁场 |
内部温度传感器 | 感应设备内部的温度(提醒用户降温,防止损伤设备) |
湿度传感器 | 感应设备是否进水(方便维修人员) |
陀螺仪 | 感应设备的持握方式(赛车类游戏) |
加速计 | 感应设备的运动(摇一摇、计步器) |
距离传感器
- (void)viewDidLoad {
[super viewDidLoad];
// 1.开启距离传感器(注意: 默认情况距离传感器是关闭的)
// [UIApplication sharedApplication].proximitySensingEnabled = YES;
// 只要开启之后, 就开始实时监听
[UIDevice currentDevice].proximityMonitoringEnabled = YES;
// 2.当监听到有物体靠近设备时系统会发出通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(proximityStateDidChange:) name:UIDeviceProximityStateDidChangeNotification object:nil];
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
// 当监听到有物体靠近设备时调用
- (void)proximityStateDidChange:(NSNotification *)note
{
// NSLog(@"%@", note);
if( [UIDevice currentDevice].proximityState)
{
NSLog(@"有物体靠近");
}else
{
NSLog(@"物体离开");
}
}
加速计
-
加速计的作用
- 用于检测设备的运动(比如摇晃)
-
加速计的经典应用场景
- 摇一摇
- 计步器
-
加速计程序的开发
- 在iOS4以前:使用UIAccelerometer,用法非常简单(到了iOS5就已经过期)
- 从iOS4开始:CoreMotion.framework
- 虽然UIAccelerometer已经过期,但由于其用法极其简单,很多程序里面都还有残留
加速计--UIAccelerometer的使用步骤
// 获得单例对象
UIAccelerometer *accelerometer = [UIAccelerometer sharedAccelerometer];
// 设置代理
accelerometer.delegate = self;
// 设置采样间隔
accelerometer.updateInterval = 1.0/30.0; // 1秒钟采样30次
// 实现代理方法
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
// acceleration中的x、y、z三个属性分别代表每个轴上的加速度
加速计--Core Motion
- 在iOS4之前,加速度计由UIAccelerometer类来负责采集数据
- 随着iPhone4的推出
- 加速度计全面升级,并引入了陀螺仪
- 与Motion(运动)相关的编程成为重头戏
- 苹果特地在iOS4中增加了专门处理Motion的框架-CoreMotion.framework
- Core Motion不仅能够提供实时的加速度值和旋转速度值,更重要的是,苹果在其中集成了很多牛逼的算法
Core Motion获取数据的两种方式
- push
- 实时采集所有数据(采集频率高)
// 1.创建运动管理者对象(强引用)
CMMotionManager *mgr = [[CMMotionManager alloc] init];
// 2.判断加速计是否可用(最好判断)
if (mgr.isAccelerometerAvailable) {
// 加速计可用
}
// 3.设置采样间隔
mgr.accelerometerUpdateInterval = 1.0/30.0; // 1秒钟采样30次
// 4.开始采样(采样到数据就会调用handler,handler会在queue中执行)
- (void)startAccelerometerUpdatesToQueue:(NSOperationQueue *)queue withHandler:(CMAccelerometerHandler)handler;
- pull
- 在有需要的时候,再主动去采集数据
// 1.创建运动管理者对象
CMMotionManager *mgr = [[CMMotionManager alloc] init];
// 2.判断加速计是否可用(最好判断)
if (mgr.isAccelerometerAvailable) { // 加速计可用 }
// 3.开始采样
- (void)startAccelerometerUpdates;
// 4.在需要的时候采集加速度数据
CMAcceleration acc = mgr.accelerometerData.acceleration;
NSLog(@"%f, %f, %f", acc.x, acc.y, acc.z);
计步器
- (void)viewDidLoad {
[super viewDidLoad];
// 1.判断计步器是否可用(iOS7开始有的API)
if (![CMStepCounter isStepCountingAvailable]) {
NSLog(@"计步器不可用");
return;
}
// 2.创建计步器对象
CMStepCounter *stepCounter = [[CMStepCounter alloc] init];
// 3.开始计步
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[stepCounter startStepCountingUpdatesToQueue:queue updateOn:5 withHandler:^(NSInteger numberOfSteps, NSDate *timestamp, NSError *error) {
if (error) return;
NSString *stepString = [NSString stringWithFormat:@"您一共走%ld步", numberOfSteps];
// [self.stepLabel performSelectorOnMainThread:@selector(setText:) withObject:stepString waitUntilDone:NO];
dispatch_async(dispatch_get_main_queue(), ^{
self.stepLabel.text = stepString;
});
}];
}