0

在全球范围内,我的游戏支持两种方向:横向右和横向左

在一个子屏幕(继承 CCLayer)中,我需要锁定当前方向,以便......当前方向被锁定......当用户弹回另一个屏幕(CCLayer)时,方向应该再次自由工作。

4

1 回答 1

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;
于 2013-05-21T07:53:40.137 回答