我正在开发一个应用程序,当用户在接近传感器周围移动时,我想打开接近传感器,并获得类似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");
}
我会按照用户的要求来做这个代码,所以我可以这样做吗?请帮我做。
- (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];
}