2013-08-23 116 views
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"); 

} 

我將在proximit做到這一點的代碼作爲每個用戶HND過來,讓鋤頭我可以做到這一點請大家幫我做到這一點。

回答

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

感謝您的回答... – Jitendra

+0

當第一次移動鄰近時,接近值接近。我neet設置計數器1. – Jitendra

+0

@Ashwinkumar雖然只有代碼的答案可能會回答這個問題,但至少有一個小小的介紹,你已經改變了什麼,以及爲什麼,通常是有用的。 – Sumurai8