2012-10-01 7 views
12

मैं एक गेम बना रहा हूं जो डिवाइस को या तो लैंडस्केप-बाएं या लैंडस्केप-दाएं ओरिएंटेशन में रहने की अनुमति देता है, और प्लेयर रोके जाने पर अभिविन्यास को बदल सकता है। जब वे करते हैं तो मुझे उस तरीके को बदलने की ज़रूरत होती है जब खेल अभिविन्यास के आधार पर एक्सेलेरोमीटर का अर्थ देता है।आईओएस 6 - ओरिएंटेशन बदलते समय कस्टम कोड कैसे चलाएं

आईओएस 5 में मैंने परिवर्तनों को पकड़ने और मेरे चर बदलने के लिए willRotateToInterfaceOrientation का उपयोग किया, लेकिन यह आईओएस 6 में बहिष्कृत है। मेरे मौजूदा कोड इस तरह दिखता है:

if(toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)  
     rect = screenRect; 

    else if(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight){ 
     rect.size = CGSizeMake(screenRect.size.height, screenRect.size.width); 
    GameEngine *engine = [GameEngine sharedEngine]; 
    if(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft){ 
     engine.orientation = -1; 
    } else { 
     engine.orientation = 1; 
    } 
} 

मैं समझता हूँ कि प्रतिस्थापन UIViewController वर्ग में viewWillLayoutSubviews तरीका है। मैं cocos2d 2.1 में इस गेम का निर्माण कर रहा हूं और डेमो प्रोजेक्ट में UIViewController क्लास नहीं दिखता है, इसलिए मैं इसे शामिल करने के तरीके और इस काम को करने के लिए कोड को कैसे देखना चाहिए, इस बारे में स्पष्ट नहीं हूं।

उत्तर

36

के लिए डिवाइस अभिविन्यास परिवर्तन सुनो:

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

जब अधिसूचित, UIDevice से डिवाइस अभिविन्यास मिलती है:

- (void)deviceOrientationDidChangeNotification:(NSNotification*)note 
{ 
    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation]; 
    switch (orientation) 
    { 
     // etc... 
    } 
} 
+3

+1 दिए गए कार्य के लिए सही विकल्प। बस सावधान रहें क्योंकि आपको फेस-अप बनाम-डाउन ओरिएंटेशन परिवर्तनों पर भी सूचित किया जाएगा और 'UIInterfaceOrientation ... 'और' UIDeviceOrientation ... ' – Till

+0

@ डैरेन के बीच का अंतर दिमाग में होगा जो मुझे चाहिए था। धन्यवाद! –

+0

@Till - फेस-अप बनाम नीचे क्या आपका मतलब है कि जब यह पोर्ट्रेट या पोर्ट्रेट में उल्टा हो जाता है? या कुछ और? साथ ही, मुझे लगता है कि डिवाइस ऑरिएन्टेशन और इंटरफेसऑरिएंटेशन लगभग हमेशा एक जैसे हैं जैसे कि कई दृश्यों के तहत विशेष परिस्थितियों के तहत, या यदि इंटरफ़ेसऑरिएंटेशन को प्रोग्रामेटिक रूप से सेट किया गया है, तो डिवाइस अभिविन्यास से मेल नहीं खाता है। या ऐसे अन्य उदाहरण हैं जिनके लिए कोई समस्या हो सकती है? –

संबंधित मुद्दे