16

मैं यह पता लगाने की कोशिश कर रहा हूं कि डिवाइस पोर्ट्रेट या लैंडस्केप मोड में है या नहीं। यदि डिवाइस का सामना नहीं हो रहा है तो मेरा कोड काफी अच्छा काम करता है। अगर यह सामना करता है (और अभिविन्यास == 5), यह चित्र और परिदृश्य के बीच अंतर नहीं करेगा। यदि UIDeviceOrientation FaceUp है तो परिदृश्य/चित्र के संदर्भ में "अभिविन्यास" निर्धारित करने के लिए वैसे भी है?UIDeviceOrientationFaceUp - चित्र और परिदृश्य के बीच अंतर कैसे करें?

मेरे कोड:

UIDeviceOrientation interfaceOrientation = [[UIDevice currentDevice] orientation]; 

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

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

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

उत्तर

37

आप UIDeviceOrientation और UIInterfaceOrientation भ्रमित नहीं होना चाहिए, वे के रूप में उनके घोषणा

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

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

UIDeviceOrientation द्वारा दिखाए गए अलग लेकिन संबंधित हैं बताता है कि उपकरण के ओरिएंटेशन है। UIInterfaceOrientation आपको बताता है कि आपके इंटरफ़ेस का अभिविन्यास क्या है, और UIViewController द्वारा उपयोग किया जाता है। UIInterfaceOrientation स्पष्ट रूप से या तो चित्र या परिदृश्य होगा, जबकि UIDeviceOrientation अस्पष्ट मूल्य (UIDeviceOrientationFaceUp, UIDeviceOrientationFaceDown, UIDeviceOrientationUnknown) हो सकता है।

किसी भी मामले में आप किसी भी डिवाइस उन्मुखीकरण है UIViewController interfaceOrientation संपत्ति अलग हो सकता है के रूप में, [[UIDevice currentDevice] orientation] के साथ एक UIViewController के उन्मुखीकरण का निर्धारण करने के प्रयास नहीं करना चाहिए (उदाहरण के लिए आपके एप्लिकेशन को सभी [[UIDevice currentDevice] orientation] पर परिदृश्य के लिए बारी बारी से नहीं करता है तो यह कर सकते हैं UIDeviceOrientationLandscapeLeft हो जबकि viewController.interfaceOrientationUIInterfaceOrientationPortrait हो सकता है)।

अद्यतन: आईओएस 8.0, [UIViewController interfaceOrientation] के रूप में बहिष्कृत किया गया है। एक विकल्प पेश किया गया here[[UIApplication sharedApplication] statusBarOrientation] है। यह UIInterfaceOrientation भी देता है।

5

मैं चाहता था & अवांछित उपकरणों झुकाव से निपटने के लिए इस कोड को कंकाल बनाने के लिए, मेरे मामले में मैं UIDeviceOrientationUnknown, UIDeviceOrientationFaceUp और UIDeviceOrientationFaceDown अनदेखी करने के लिए, पिछले अनुमति उन्मुखीकरण कैशिंग चाहते हैं। यह कोड आईफोन और आईपैड उपकरणों से संबंधित है और आपके लिए उपयोगी हो सकता है।

- (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 */ 
     } 
    } 
} 
संबंधित मुद्दे