从webview返回后自动旋转屏幕错误

时间:2011-05-15 01:36:52

标签: iphone objective-c ios cocos2d-iphone

我使用cocos结合admob,nomarl我的app工作得很好,但是在点击广告并返回游戏后,它布局错误

这是我的轮换代码

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return ( UIInterfaceOrientationIsLandscape( interfaceOrientation ) );
}

-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    CGRect screenRect = [[UIScreen mainScreen] bounds];
    CGRect rect;

    if (toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)     
        rect = screenRect;
else if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
        rect.size = CGSizeMake(screenRect.size.height, screenRect.size.width);

    CCDirector *director = [CCDirector sharedDirector];
    EAGLView *glView = [director openGLView];
    float contentScaleFactor = [director contentScaleFactor];

    if (contentScaleFactor != 1) {
        rect.size.width *= contentScaleFactor;
        rect.size.height *= contentScaleFactor;
    }
    glView.frame = rect;
}

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

之前我遇到过类似的问题,我做错了,我创建了一个新的UIViewController,并为AdMob视图设置为rootViewController。我现在在我的应用程序中所做的是:

adMobView.rootViewController = [RootViewController sharedInstance];
[[[CCDirector sharedDirector] openGLView] addSubview:adMobView];

其中[RootViewController sharedInstance]是一个类方法,它返回应用程序中唯一的RootViewController实例。请参阅https://stackoverflow.com/a/10222956/1241690

(对于cocos2d 2.x,第二行应为:

[[[CCDirector sharedDirector] view] addSubview:adMobView];