我正在开发一个应用程序,我希望当用户在接近传感器周围移动并获得接近状态值为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");
}
我将根据用户在proximit中执行此代码,所以我可以帮助我这样做。
答案 0 :(得分: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];
}