码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2017-05-18 01:38:08      阅读:333      评论:0      收藏:0      [点我收藏+]

标签:device   sts   enter   打开   距离   set   from   ios   智能手机   

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

 

●  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

 



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

标签:device   sts   enter   打开   距离   set   from   ios   智能手机   

原文地址:http://www.cnblogs.com/liuwj/p/6869983.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!