传感器

// 距离传感器

// 温度传感器

// 磁力传感器

// 光传感器

// 湿度传感器

// 陀螺仪  -- 导航, 赛车游戏(角速度传感器)

// 运动传感器 - 加速度传感器  --  摇一摇, 计步器

//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    

    // 距离传感器 - 默认是关闭的

    // 1. 开启距离传感器 - 开启之后就会实时监听

    [UIDevice currentDevice].proximityMonitoringEnabled = YES;

    

    // 2. 当监听到有物体靠近设备时, 会调用通知

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ProximityStateDidChanged:) name:UIDeviceProximityStateDidChangeNotification object:nil];

    

}

- (void)ProximityStateDidChanged:(NSNotification *)noticefication

{

    LogRed(@"%@",noticefication);

    if ([UIDevice currentDevice].proximityState){

        LogGreen(@"靠近-----靠近");

    }else{

        LogMagenta(@"离开 ---  离开");

    }

    

}

- (void)dealloc

{

    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceProximityStateDidChangeNotification object:nil];

}

@end

原文地址:https://www.cnblogs.com/guangleijia/p/4833095.html