传感器使用

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 默认情况下传感器处于关闭状态
    [UIDevice currentDevice].proximityMonitoringEnabled = YES;
    // 添加到通知中心,状态改变调用方法
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(proximityStateDidChange) name:UIDeviceProximityStateDidChangeNotification object:nil];
}

- (void)proximityStateDidChange {
    // 用距离传感器判断是否有东西靠近还是离开
    if ([UIDevice currentDevice].proximityState) {
        NSLog(@"有东西靠近");
    } else {
        NSLog(@"有东西离开");
    }
}

 

posted @ 2015-08-10 13:54  小小代码块  阅读(54)  评论(0编辑  收藏  举报