2012-08-23 5 views
4

UIDeviceOrientationFaceDown을 감지하고 싶습니다. 내가 어떻게 할 수 있니? 실제로 iphone 얼굴이 평평한 표면에있을 때 몇 가지 작업을 수행하고 싶습니다. 어떻게 가능합니까 ??? plz이 일을 도와주세요.iphone의 UIDeviceOrientationFaceDown

내가이 코드를 가지고 있지만, 어디에 있는지, 어떤 튜토리얼은 다음 날을 제안 해주십시오있을 경우이 코드를

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; 

BOOL getOrientationUpdates = [[UIDevice currentDevice] isGeneratingDeviceOrientationNotifications]; 
    NSLog(@"will receive orientation notifications: %@", [email protected]"YES":@"NO"); 

    [[NSNotificationCenter defaultCenter] addObserver:self 
            selector:@selector(orientationChanged:) 
             name:UIDeviceOrientationDidChangeNotification 
             object:nil]; 

을 어떻게 적용되는지를하지 않는

미리 감사하고 소중한에 환영 도움과 제안.

+0

얼굴을 아래로 향하게 할 때, 물건 즉 표를 아래로 향하게합니다. 아니면 공중에서 얼굴을 마주 치지 않습니까? – geminiCoder

+0

테이블과 같은 모든 물체를 아래로 향하게합니다.이 경우 – Rox

+1

은 근위 검열기를 사용하십시오. 전화기가 귀에 붙었을 때이를 감지하는 가벼운 센서. http://learningiphoneprogramming.com/pdf/The_Sensors_in_your_iPhone.pdf – geminiCoder

답변

8

iPhone의 ProximityState를 감지하여이를 수행 할 수 있습니다. [UIDevice currentDevice] 싱글 톤을 사용하여 proximityMonitoringEnabledYES으로 설정하십시오. proximityState 속성을 통해 근접 정보에 액세스 할 수 있습니다.

[[UIDevice currentDevice]proximityState]; 

아이폰이 센서는 통화 중에 귀에 넣어 화면이 꺼집니다있다, AFAIK 그 적외선 센서입니다. 액세스 할 수 있습니다.

편집 : 다음 코드를 사용하여이를 수행 할 수도 있습니다. UIDeviceOrientationFaceDown The device is held parallel to the ground with the screen facing downwards. (iPhone이 객체에 닿았는지 여부를 알고 싶다면 객체에 접촉했는지 여부에 관계없이) 기기의 근접 상태를 감지합니다.

[[NSNotificationCenter defaultCenter] removeObserver:self]; 
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(detectOrientation) name:@"UIDeviceOrientationDidChangeNotification" object:nil]; 


    -(void)detectOrientation;{ 

      switch ([[UIDevice currentDevice] orientation]) { 
       case UIDeviceOrientationPortrait: 
       { 
        NSLog(@"portrait"); 
       } 
        break; 
       case UIDeviceOrientationPortraitUpsideDown: 
       { 
        NSLog(@"portraitUpSideDown"); 
       } 
        break; 
       case UIDeviceOrientationLandscapeLeft: 
       { 
        NSLog(@"landscapeLeft"); 
       } 
        break; 
       case UIDeviceOrientationLandscapeRight: 
       { 
        NSLog(@"landscapeRight"); 
       } 
       break; 
       case UIDeviceOrientationFaceDown: 
       { 
        NSLog(@"facedown!!"); 
       } 
       break; 

      default: 
       break; 
     } 
    } 

} 

편집 : 의견에 질문에 답하십시오.

[UIDevice currentDevice].proximityMonitoringEnabled = YES; 
[[NSNotificationCenter defaultCenter] removeObserver:self]; 
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleProximityChangeNotification:) name:UIDeviceProximityStateDidChangeNotification object:nil]; 

다음 방법을

-(void)handleProximityChangeNotification{ 
    if([[UIDevice currentDevice]proximityState]){ 
     NSLog(@"..."); 
    } 
} 
+0

괜찮아요 .. 알아 냈습니다.하지만 proximityState를 얻으려면 viewController에 코드를 넣습니다. – Rox

+0

장치가 객체에 접촉했는지 알고 싶습니까? – janusbalatbat

+0

예, 알고 싶습니다. – Rox

2

물품하여 viewDidLoad에이 라인을 추가 가속도 센서에서의 Z 값을 사용하여 (-1 < = Z < = 1). 기기가 아래로 향하게되면 z 값은 0 < z < = 1 (지상과 평행을 이루는 경우 0, 지상에 수직을 향한 경우 1)이됩니다. 당신이

#import <CoreMotion/CoreMotion.h> 

CMMotionManager *cm=[[CMMotionManager alloc] init]; 
cm.deviceMotionUpdateInterval=0.2f; 
[cm startDeviceMotionUpdatesToQueue:[NSOperationQueue mainQueue] 
          withHandler:^(CMDeviceMotion *data, NSError *error) { 
           if(data.gravity.z>=0.3f)// 

           { 
           //DO something 
           } 

    }]; 

CMMotionManager 프로젝트에 CoreMotion 프레임 워크를 추가하는 것을 잊지 마세요 사용할 수있는 데이터를 얻을 수 있습니다.