16

기기가 세로 모드인지 가로 모드인지 확인하려고합니다. 장치가 위를 향하지 않으면 내 코드가 잘 작동합니다. 앞면이 보이면 (방향 == 5) 세로와 가로를 구별하지 않습니다. UIDeviceOrientation이 FaceUp 인 경우 가로/세로의 관점에서 "방향"을 결정하는 방법이 있습니까?UIDeviceOrientationFaceUp - 세로와 가로를 구분하는 방법은 무엇입니까?

내 코드 : 자신의 선언

typedef enum { 
    UIDeviceOrientationUnknown, 
    UIDeviceOrientationPortrait, 
    UIDeviceOrientationPortraitUpsideDown, 
    UIDeviceOrientationLandscapeLeft, 
    UIDeviceOrientationLandscapeRight, 
    UIDeviceOrientationFaceUp, 
    UIDeviceOrientationFaceDown 
} UIDeviceOrientation; 

typedef enum { 
    UIInterfaceOrientationPortrait   = UIDeviceOrientationPortrait, 
    UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown, 
    UIInterfaceOrientationLandscapeLeft  = UIDeviceOrientationLandscapeRight, 
    UIInterfaceOrientationLandscapeRight  = UIDeviceOrientationLandscapeLeft 
} UIInterfaceOrientation; 

UIDeviceOrientation 같이

UIDeviceOrientation interfaceOrientation = [[UIDevice currentDevice] orientation]; 

NSLog(@"orientation: %d", interfaceOrientation); 

if (interfaceOrientation == UIDeviceOrientationIsLandscape(interfaceOrientation)) { 
    NSLog(@"LANDSCAPE!!!"); 
} 

if (interfaceOrientation == UIDeviceOrientationIsPortrait(interfaceOrientation)) { 
    NSLog(@"PORTRAIT!!!"); 
} 

답변

37

당신은 UIDeviceOrientationUIInterfaceOrientation을 혼동해서는 안, 그들은 장치의 방향이 무엇인지를 알려줍니다 다르지만 관련이 있습니다. UIInterfaceOrientation은 인터페이스의 방향이 무엇인지 알려주고 UIViewController에 의해 사용됩니다. UIInterfaceOrientation은 분명히 세로 또는 가로입니다. 반면 UIDeviceOrientation은 모호한 값 (UIDeviceOrientationFaceUp, UIDeviceOrientationFaceDown, UIDeviceOrientationUnknown)을 가질 수 있습니다. 어떤 경우

는 장치의 방향이 UIViewController interfaceOrientation 속성이 다를 수 있습니다 무엇인지에 상관없이 같은 [[UIDevice currentDevice] orientation]와의 UIViewController의 방향을 결정하지한다 (예를 들어 앱의 모든 [[UIDevice currentDevice] orientation]에서 가로로 회전하지 않는 경우 수 UIDeviceOrientationLandscapeLeft 일 수 있으며, viewController.interfaceOrientationUIInterfaceOrientationPortrait 일 수 있습니다.

업데이트 : iOS 8.0 현재, [UIViewController interfaceOrientation]은 더 이상 사용되지 않습니다. 대안으로 here[[UIApplication sharedApplication] statusBarOrientation]입니다. 또한 UIInterfaceOrientation을 반환합니다.

5

나는 내 경우에는 내가 마지막 허용 방향을 캐싱의 UIDeviceOrientationUnknown, UIDeviceOrientationFaceUp 및 UIDeviceOrientationFaceDown을 무시하려면, 원 & 원하지 않는 장치의 방향을 다루는이 코드 골격을 만든다. 이 코드는 iPhone 및 iPad 장치를 다루며 유용 할 수 있습니다.

- (void)modXibFromRotation { 

    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation]; 
    NSString *device = [[UIDevice currentDevice]localizedModel]; 
    UIInterfaceOrientation cachedOrientation = [self interfaceOrientation]; 

    if ([device isEqualToString:@"iPad"]) { 

     if (orientation == UIDeviceOrientationUnknown || 
      orientation == UIDeviceOrientationFaceUp || 
      orientation == UIDeviceOrientationFaceDown) { 

       orientation = (UIDeviceOrientation)cachedOrientation; 
     } 

     if (orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight) { 

      /* Your code */ 
     } 

     if (orientation == UIDeviceOrientationPortrait || orientation == UIDeviceOrientationPortraitUpsideDown) { 

      /* Your code */  
     } 
    } 

    if ([device isEqualToString:@"iPhone"] || [device isEqualToString:@"iPod"]) { 

     if (orientation == UIDeviceOrientationUnknown || 
     orientation == UIDeviceOrientationFaceUp || 
     orientation == UIDeviceOrientationFaceDown) { 

      orientation = (UIDeviceOrientation)cachedOrientation; 
     } 

     if (orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight) { 

      /* Your code */ 
     } 

     if (orientation == UIDeviceOrientationPortrait || orientation == UIDeviceOrientationPortraitUpsideDown) { 

      /* Your code */ 
     } 
    } 
} 
관련 문제