在代码身上,用心去飞翔

你是我的小啊小code,怎么爱你都不嫌多

cocos2d 3.2版本IOS因键盘引起横竖屏问题

在cocos2d 3.2版本中,在IOS8.0以上游戏才用横竖屏都支持后,弹出输入法,或者游戏切到后台弹出输入法再切回来会引起屏幕跑偏,修复方案如下:

修改CCEAGLView.mm文件,注意第一个case的处理

-(void) doAnimationWhenKeyboardMoveWithDuration:(float)duration distance:(float)dis
{
    [UIView beginAnimations:nil context:nullptr];
	[UIView setAnimationDelegate:self];
	[UIView setAnimationDuration:duration];
	[UIView setAnimationBeginsFromCurrentState:YES];
    
    //NSLog(@"[animation] dis = %f, scale = %f \n", dis, cocos2d::GLView::getInstance()->getScaleY());
    
    if (dis < 0.0f) dis = 0.0f;

    auto glview = cocos2d::Director::getInstance()->getOpenGLView();
	dis *= glview->getScaleY();
    
    if (self.contentScaleFactor == 2.0f)
    {
        dis /= 2.0f;
    }
    
    switch ([self getstatusBarOrientation])
    {
        case UIInterfaceOrientationPortrait:
            if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
            {
                self.frame = CGRectMake(originalRect_.origin.x, originalRect_.origin.y - dis, self.frame.size.width, self.frame.size.height);
            }else{
                self.frame = CGRectMake(originalRect_.origin.x, originalRect_.origin.y - dis, originalRect_.size.width, originalRect_.size.height);
            }
            break;
            
        case UIInterfaceOrientationPortraitUpsideDown:
            self.frame = CGRectMake(originalRect_.origin.x, originalRect_.origin.y + dis, originalRect_.size.width, originalRect_.size.height);
            break;
            
        case UIInterfaceOrientationLandscapeLeft:
            self.frame = CGRectMake(originalRect_.origin.x - dis, originalRect_.origin.y , originalRect_.size.width, originalRect_.size.height);
            break;
            
        case UIInterfaceOrientationLandscapeRight:
            self.frame = CGRectMake(originalRect_.origin.x + dis, originalRect_.origin.y , originalRect_.size.width, originalRect_.size.height);
            break;
            
        default:
            break;
    }
    
	[UIView commitAnimations];
}

在其他cocos版本中,采用以下方法处理:

-(void) doAnimationWhenKeyboardMoveWithDuration:(float)duration distance:(float)dis
{
    [UIView beginAnimations:nil context:nullptr];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDuration:duration];
    [UIView setAnimationBeginsFromCurrentState:YES];
 
    //NSLog(@"[animation] dis = %f, scale = %f \n", dis, cocos2d::GLView::getInstance()->getScaleY());   
    if (dis < 0.0f) dis = 0.0f;
    auto glview = cocos2d::Director::getInstance()->getOpenGLView();
    dis *= glview->getScaleY();   
    dis /= self.contentScaleFactor;
 
    switch (getFixedOrientation([[UIApplication sharedApplication] statusBarOrientation]))
    {
        case UIInterfaceOrientationPortrait:
            if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
            {
                self.frame = CGRectMake(originalRect_.origin.x, originalRect_.origin.y - dis, self.frame.size.width, self.frame.size.height);
            }else{
                self.frame = CGRectMake(originalRect_.origin.x, originalRect_.origin.y - dis, originalRect_.size.width, originalRect_.size.height);
            }
            break;           
 
        case UIInterfaceOrientationPortraitUpsideDown:
            self.frame = CGRectMake(originalRect_.origin.x, originalRect_.origin.y + dis, originalRect_.size.width, originalRect_.size.height);
            break;           
 
        case UIInterfaceOrientationLandscapeLeft:
            self.frame = CGRectMake(originalRect_.origin.x - dis, originalRect_.origin.y , originalRect_.size.width, originalRect_.size.height);
            break;           
 
        case UIInterfaceOrientationLandscapeRight:
            self.frame = CGRectMake(originalRect_.origin.x + dis, originalRect_.origin.y , originalRect_.size.width, originalRect_.size.height);
            break;
 
        default:
            break;
    }
    [UIView commitAnimations];
}


阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhenyu5211314/article/details/52367182
文章标签: ios cocos2d 横竖屏
个人分类: Cocos2d
想对作者说点什么? 我来说一句

cocos2d-x-3.2旧版引擎下载

2017年09月17日 49B 下载

没有更多推荐了,返回首页

不良信息举报

cocos2d 3.2版本IOS因键盘引起横竖屏问题

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭