2010-06-09 14 views
11

मैं प्रत्येक पृष्ठ के लिए UITabBarController और UIViewControllers के आधार पर एक आईफोन एप्लिकेशन पर काम कर रहा हूं। अनुप्रयोग, केवल पोर्ट्रेट मोड में चलाने के लिए की जरूरत है ताकि हर दृश्य नियंत्रक + एप्लिकेशन प्रतिनिधि कोड की इस पंक्ति के साथ चला जाता:मैन्युअल रूप से परिदृश्य में रोटेशन का पता लगाने

- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation { 
return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 

वहाँ एक दृश्य नियंत्रक जब iPhone rotaed वह जगह है जहाँ मैं एक UIImageView पॉप अप करना चाहते हैं परिदृश्य के लिए। छवि का डिज़ाइन परिदृश्य दिखता है, हालांकि चौड़ाई और ऊंचाई 320x460 (इसलिए इसका चित्र) है।

मैं इस प्रकार के घूर्णन को मैन्युअल रूप से कैसे पहचान सकता/सकती हूं, बस इस विशिष्ट दृश्य नियंत्रक में, पूरे दृश्य पर ऑटो रोटेशन के साथ?

थॉमस

अद्यतन:

धन्यवाद!

मैं viewDidLoad में इस श्रोता कहा:

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

didRotate इस तरह दिखता है:

- (void) didRotate:(NSNotification *)notification 

    { 
     UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation]; 

     if (orientation == UIDeviceOrientationLandscapeLeft) 
     { 
      //your code here 

     } 
    } 

उत्तर

20

मैं जरूरत है कि एक पुराने परियोजना में - आशा है कि यह अभी भी काम करता है ...

1) एक अधिसूचना पंजीकृत करें:

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; 
[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(detectOrientation) 
              name:UIDeviceOrientationDidChangeNotification 
              object:nil]; 

2) तो फिर तुम परिवर्तन पर रोटेशन के खिलाफ परीक्षण कर सकते हैं:

-(void) detectOrientation { 
    if (([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft) || 
     ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight)) { 
     [self doLandscapeThings]; 
    } else if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortrait || [[UIDevice currentDevice] orientation] == UIDeviceOrientationPortraitUpsideDown) { 
     [self doPortraitThings]; 
    } 
} 

आशा है कि मदद करता है!

+3

धन्यवाद आग नहीं होगा! मैंने इस श्रोता को दृश्य में जोड़ा हैडलोड: [[NSNotificationCenter defaultCenter] addObserver: स्वयं चयनकर्ता: @ चयनकर्ता (didRotate:) नाम: UIDeviceOrientationDidChangeNotification ऑब्जेक्ट: nil]; didRotate इस तरह दिखता है: - (शून्य) didRotate: (NSNotification *) अधिसूचना {\t \t UIDeviceOrientation उन्मुखीकरण = [[UIDevice currentDevice] उन्मुखीकरण]; \t \t अगर (उन्मुखीकरण == UIDeviceOrientationLandscapeLeft) \t { \t \t // यहाँ कोड \t} } –

+1

माइनर में सुधार, अपने स्ट्रिंग मान के बजाय लगातार UIDeviceOrientationDidChangeNotification का उपयोग (जो @ "UIDeviceOrientationDidChangeNotification") –

+0

घटना के लिए श्रोता के रूप में खुद को पंजीकरण रद्द करने के लिए मत भूलना ... – aroth

2

Comic Sans जवाब देने के लिए बेहतर कोड के नीचे होगा .. उसका कोड हमेशा सही ढंग से (केवल 80 मेरे परीक्षण में समय की%)

-(void) detectOrientation { 
    if (UIDeviceOrientationIsLandscape([[UIDevice currentDevice] orientation])) { 
     [self setupForLandscape]; 
    } else if (UIDeviceOrientationIsPortrait([[UIDevice currentDevice] orientation])) { 
     [self setupForPortrait]; 
    } 
}