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

前端之家收集整理的这篇文章主要介绍了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_.size.width,originalRect_.size.height);
            }
            break;
            
        case UIInterfaceOrientationPortraitUpsideDown:
            self.frame = CGRectMake(originalRect_.origin.x,originalRect_.origin.y + dis,originalRect_.size.height);
            break;
            
        case UIInterfaceOrientationLandscapeLeft:
            self.frame = CGRectMake(originalRect_.origin.x - dis,originalRect_.origin.y,originalRect_.size.height);
            break;
            
        case UIInterfaceOrientationLandscapeRight:
            self.frame = CGRectMake(originalRect_.origin.x + dis,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,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_.size.height);
            }
            break;           
 
        case UIInterfaceOrientationPortraitUpsideDown:
            self.frame = CGRectMake(originalRect_.origin.x,originalRect_.size.height);
            break;           
 
        case UIInterfaceOrientationLandscapeLeft:
            self.frame = CGRectMake(originalRect_.origin.x - dis,originalRect_.size.height);
            break;           
 
        case UIInterfaceOrientationLandscapeRight:
            self.frame = CGRectMake(originalRect_.origin.x + dis,originalRect_.size.height);
            break;
 
        default:
            break;
    }
    [UIView commitAnimations];
}
原文链接:https://www.f2er.com/cocos2dx/339260.html

猜你在找的Cocos2d-x相关文章