1

我正在开发一个应用程序,我想在用户移动接近传感器时打开接近传感器并获得接近状态值,如 0。当用户移动手第二时间值得到 1 我该怎么做我知道如何启用接近但我想在用户移动代理时启用接近度。那么我该怎么做我尝试了这段代码。启用接近。

UIDevice * device=[UIDevice currentDevice];
    device.proximityMonitoringEnabled=YES;

    if (device.proximityMonitoringEnabled)
    {
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(handleProximityChange:)
                                                     name:UIDeviceProximityStateDidChangeNotification
                                         object:nil];
    }
    else
    {
        NSLog(@"Device not capable to support Proximity sensor");
        // device not capable
    }

并在此方法中捕获接近状态...

-(void)handleProximityChange:(NSNotification *)notification
{

    NSLog(@"Proximity event catch");

}

我将按照用户 hnd 在 proximit 中执行此代码,所以我可以执行此操作,请帮助我执行此操作。

4

1 回答 1

3
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    count = 0;
    condition = 0;
    [self addProximitySensorControl];
}

-(void)addProximitySensorControl {

    UIDevice *device = [UIDevice currentDevice];
    device.proximityMonitoringEnabled = YES;

    BOOL state = device.proximityState;
    if(state)
    {
        NSLog(@"YES");
        _sensorSupport.text = @"YES";
    }
    else
    {
        NSLog(@"NO");
     _sensorSupport.text = @"No";
    }

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(proximityChanged:)
                                                 name:@"UIDeviceProximityStateDidChangeNotification"
                                               object:nil];
}
-(void)proximityChanged:(NSString*)str
{
    NSLog(@"i am in proximityChanged");
    condition++;
    if (condition % 2 == 0) {
        count++;
    }
    _counter.text = [NSString stringWithFormat:@"%d",count];
}
于 2013-08-24T07:07:22.810 回答