zoukankan      html  css  js  c++  java
  • (九十一)距离传感器的使用

    iPhone上的距离传感器通过UIDevice开启,开启后系统以Notification的方式通知。

    ①开启距离传感器,监听通知:

    - (void)viewDidLoad {
        [super viewDidLoad];
    
        // 开启传感器在iOS3之后通过UIDevice的currentDevice的proximityMonitoringEnabled设置
        [UIDevice currentDevice].proximityMonitoringEnabled = YES;
        
        // 监听到物体靠近时,系统会发出通知
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(proximityStateChange:) name:UIDeviceProximityStateDidChangeNotification object:nil];
        
    }

    ②实现通知方法:当物体靠近距离传感器时,手机会自动锁屏,并且proximityState为YES,否则为NO。

    - (void)proximityStateChange:(NSNotification *)nof{
        
        NSLog(@"%d",[UIDevice currentDevice].proximityState);
        if ([UIDevice currentDevice].proximityState) {
            NSLog(@"靠近");
        }else{
            NSLog(@"离开");
        }
        
    }

    ③dealloc时要移除通知:

    - (void)dealloc{
        
        [[NSNotificationCenter defaultCenter] removeObserver:self];
        
    }


  • 相关阅读:
    给列表项标记添加自定义图像
    双飞翼布局与圣杯布局
    CSS3 calc()
    CSS滚动视差
    应用层层面面试题汇总
    Linux下OpenSSL 安装
    深入理解:Android 编译系统
    ios 好去处
    IBOutlet & IBAction
    ar技术序章-SDK介绍和选择
  • 原文地址:https://www.cnblogs.com/aiwz/p/6154104.html
Copyright © 2011-2022 走看看