码迷,mamicode.com
首页 > 其他好文 > 详细

(九十一)距离传感器的使用

时间:2015-07-27 23:01:59      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:oc   ios   距离传感器   

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];
    
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

(九十一)距离传感器的使用

标签:oc   ios   距离传感器   

原文地址:http://blog.csdn.net/xyt8023y/article/details/47092063

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