IOS UIDevice距离传感器(打开 关闭)

●   什么是传感器
●  传感器是一种感应\检测装置, 目前已经广泛应用于智能手机上

 

●   iPhone5中内置的传感器有

 ●  运动传感器\加速度传感器\加速计(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(@"某个物体远离了设备屏幕"); // 屏幕会自动解锁

} }

//打开距离感应功能

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    // 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(@"物体离开");
   }
}




@end
View Code

 



转载于:https://www.cnblogs.com/liuwj/p/6869983.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值