消息关闭
    暂无新消息!

iOS转屏偶尔只在第一次转屏时失败

问题作者 : 波板糖2017-06-05发布
     游戏中,启动后的场景A是竖屏的,从场景A进入场景B时,游戏强制转为横屏,如果从场景B返回场景A,又要转回竖屏。
    现在出现一个BUG,偶尔会在启动程序后第一次从场景A进入场景B时,转屏失败,但是返回场景A就不会有问题,而且马上再一次进入场景B都会正常转换了。
    转换横竖屏的时候,正常转换的话,导航栏也会跟着转换,转换失败时,导航栏是没有变,但是输出屏幕方向信息确是和转换成功时一样的( [UIApplication sharedApplication].statusBarOrientation)
    重点: 
        1、BUG只在启动后的第一次转横屏时候出现,转回竖屏或者第一次以后是一定不会出现的
        2、BUG不是必定出现的。
        3、不管成功还是失败都进入了转屏函数,并且在函数中打印各种width、height、statusBarOrientation等等都是一样的

        if (self.landSceneController != nil) {
            return ;
        }
        NSLog(@"======= 转成横屏 =======");
        
        //打印设备方向状态
        NSLog(@"interface = %zi", [UIApplication sharedApplication].statusBarOrientation);

        id  eaglView =  self.viewController.view ;
        self.viewController.view = nil;
        if (self.landSceneController == nil) {
            self.landSceneController = [[LandSceneController alloc] initWithNibName:nil bundle:nil];
            self.landSceneController.wantsFullScreenLayout = YES;
        }
        self.landSceneController.view = eaglView;
        
        [window setRootViewController:self.landSceneController];
        
        [self.viewController presentViewController:self.landSceneController animated:NO completion:nil];
        [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];

        CGRect screenBounds = [UIScreen mainScreen].bounds;
        if(screenBounds.size.height > screenBounds.size.width)
        {
            int temp = screenBounds.size.height;
            screenBounds.size.height = screenBounds.size.width;
            screenBounds.size.width = temp;
        }
        cocos2d::GLView *glview = cocos2d::Director::getInstance()->getOpenGLView();
        if (glview)
        {
            CCEAGLView *eaglview = (CCEAGLView*) glview->getEAGLView();
            [eaglview setBounds:CGRectMake(0, 0, screenBounds.size.width, screenBounds.size.height)];            
            if (eaglview)
            {
                CGSize s = CGSizeMake([eaglview getWidth], [eaglview getHeight]);
                cocos2d::Application::getInstance()->applicationScreenSizeChanged(int(s.width), int(s.height));
            }
        }
        //打印设备方向状态
        NSLog(@"interface = %zi", [UIApplication sharedApplication].statusBarOrientation);
        [self.viewController release];
        self.viewController = nil;

2个回答

︿ 0
其实我怀疑是首个viewController 初始化的时候出问题,但是去检查时似乎又没发现出错(估计是我技术不好)
补充初始化代码:
    cocos2d::Application *app = cocos2d::Application::getInstance();
    app->initGLContextAttrs();
    cocos2d::GLViewImpl::convertAttrs();
    
    sharedInstance = self;
    _viewDelegate = [[AppController alloc] init];
    
    window = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds]];

    CCEAGLView *eaglView = [CCEAGLView viewWithFrame: [window bounds]
                                         pixelFormat: (NSString*)cocos2d::GLViewImpl::_pixelFormat
                                         depthFormat: cocos2d::GLViewImpl::_depthFormat
                                  preserveBackbuffer: NO
                                          sharegroup: nil
                                       multiSampling: NO
                                     numberOfSamples: 0 ];
    
    [eaglView setMultipleTouchEnabled:YES];
    
    self.viewController = [[RootViewController alloc] initWithNibName:nil bundle:nil];
    self.viewController.wantsFullScreenLayout = YES;
    self.viewController.view = eaglView;
    
    [window setRootViewController:_viewController];
    
    [window makeKeyAndVisible];
    
    [[UIApplication sharedApplication] setStatusBarHidden: YES];
    [[UIApplication sharedApplication] setIdleTimerDisabled: YES];
    
    cocos2d::GLView *glview = cocos2d::GLViewImpl::createWithEAGLView(eaglView);
    cocos2d::Director::getInstance()->setOpenGLView(glview);