在全球范围内,我的游戏支持两种方向:右侧风景和左侧风景
在一个子屏幕(继承CCLayer)中,我需要锁定当前方向,以便...当前方向被锁定... 当用户弹回另一个屏幕(CCLayer)时,方向应该再次自由工作。
答案 0 :(得分:1)
我是这样做的:
编辑AppDelegate.h,添加一个用于锁定方向的掩码:
@interface MyNavigationController : UINavigationController <CCDirectorDelegate>
@property UIInterfaceOrientationMask lockedToOrientation;
@end
在AppDelegate.m中,合成掩码,并替换两个函数:
@synthesize lockedToOrientation; // assign
-(NSUInteger)supportedInterfaceOrientations {
if (!self.lockedToOrientation) {
// iPhone only
if( [[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone )
return UIInterfaceOrientationMaskLandscape;
// iPad only
return UIInterfaceOrientationMaskLandscape;
}
else {
return self.lockedToOrientation;
}
}
// Supported orientations. Customize it for your own needs
// Only valid on iOS 4 / 5. NOT VALID for iOS 6.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if (!self.lockedToOrientation) {
// iPhone only
if( [[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone )
return UIInterfaceOrientationIsLandscape(interfaceOrientation);
// iPad only
// iPhone only
return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}
else {
// I don't need to change this at this point
return NO;
}
}
然后每当我需要将界面锁定到某个方向时,我都会在appdelegate中访问navController。检查其interfaceOrientation属性并相应地设置锁定掩码
AppController* appdelegate = (AppController*)[UIApplication sharedApplication].delegate;
const UIDeviceOrientation ORIENTATION = appdelegate.navController.interfaceOrientation;
appdelegate.navController.lockedToOrientation = ORIENTATION == UIInterfaceOrientationLandscapeLeft ? UIInterfaceOrientationMaskLandscapeLeft : UIInterfaceOrientationMaskLandscapeRight;
在dealloc中,或者每当我想要取下锁时,我都会这样做:
AppController* appdelegate = (AppController*)[UIApplication sharedApplication].delegate;
appdelegate.navController.lockedToOrientation = 0;