cocos2d iphone - Disallow orientation in a certain CCLayer only -
globally game supports 2 orientations: landscape right , landscape left
in 1 subscreen (inheriting cclayer) need lock current orientation ... current orientation locked... when user pops screen (cclayer), orientation should work freely again.
i did this:
edit appdelegate.h, add mask locking orientation:
@interface mynavigationcontroller : uinavigationcontroller <ccdirectordelegate> @property uiinterfaceorientationmask lockedtoorientation; @end
in appdelegate.m, synthesize mask, , replace 2 functions:
@synthesize lockedtoorientation; // assign -(nsuinteger)supportedinterfaceorientations { if (!self.lockedtoorientation) { // iphone if( [[uidevice currentdevice] userinterfaceidiom] == uiuserinterfaceidiomphone ) return uiinterfaceorientationmasklandscape; // ipad return uiinterfaceorientationmasklandscape; } else { return self.lockedtoorientation; } } // supported orientations. customize own needs // valid on ios 4 / 5. not valid ios 6. - (bool)shouldautorotatetointerfaceorientation:(uiinterfaceorientation)interfaceorientation { if (!self.lockedtoorientation) { // iphone if( [[uidevice currentdevice] userinterfaceidiom] == uiuserinterfaceidiomphone ) return uiinterfaceorientationislandscape(interfaceorientation); // ipad // iphone return uiinterfaceorientationislandscape(interfaceorientation); } else { // don't need change @ point return no; } }
then whenever need lock interface orientation, access navcontroller in appdelegate. check interfaceorientation property , set locked mask accordingly
appcontroller* appdelegate = (appcontroller*)[uiapplication sharedapplication].delegate; const uideviceorientation orientation = appdelegate.navcontroller.interfaceorientation; appdelegate.navcontroller.lockedtoorientation = orientation == uiinterfaceorientationlandscapeleft ? uiinterfaceorientationmasklandscapeleft : uiinterfaceorientationmasklandscaperight;
in dealloc or whenever wanna remove lock, this:
appcontroller* appdelegate = (appcontroller*)[uiapplication sharedapplication].delegate; appdelegate.navcontroller.lockedtoorientation = 0;
Comments
Post a Comment