cocos2dx适配phonex

1、引入Launch Screen Storyboard布局文件

2、如果需要获得安全页面展示(留出刘海区域,防止刘海遮挡)

RootViewController.mm添加viewSafeAreaInsetsDidChange函数

- (void)viewSafeAreaInsetsDidChange {
    [super viewSafeAreaInsetsDidChange];
    NSLog(@"viewSafeAreaInsetsDidChange %@",NSStringFromUIEdgeInsets(self.view.safeAreaInsets));
    [self updateOrientation];
}

bool changeViewFrameV = false;
- (void)updateOrientation {
    if (@available(iOS 11.0, *))
    {
        CGRect rect = [[UIScreen mainScreen]bounds];
        CGSize size = rect.size;
        CGFloat width = size.width;
        CGFloat height = size.height;
        CGFloat scale_screen = [UIScreen mainScreen].scale;
        
        if (self.view and !changeViewFrameV)
        {
            //根据分辨率判断手机型号
            if (width*scale_screen == 2436 and height*scale_screen == 1125)//iPhoneX,iphoneXS
            {
                //在标准的safeAreaInsets区域的基础上再拓宽一定范围   shanwl
//                UIEdgeInsets a = self.view.layoutMargins;
//                UIEdgeInsets b = self.view.safeAreaInsets;
                self.view.frame = CGRectMake(self.view.safeAreaInsets.left - 13,
                                     0,
                                     self.view.frame.size.width - self.view.safeAreaInsets.left - self.view.safeAreaInsets.right + 26,
                                     self.view.frame.size.height);
            }
            else if ((width*scale_screen == 1792 and height*scale_screen == 828) or //iphone XR
                     (width*scale_screen == 2688 and height*scale_screen == 1242)) //iphone XS MAX
            {
                //标准的safeAreaInsets区域
                self.view.frame = CGRectMake(self.view.safeAreaInsets.left,
                                      0,
                                      self.view.frame.size.width - self.view.safeAreaInsets.left - self.view.safeAreaInsets.right,
                                      self.view.frame.size.height);
            }
            changeViewFrameV = true;
        }
    }
}

3、如果有横竖屏切换的需求

在AppController.mm 函数didFinishLaunchingWithOptions中加入

//横屏初始化
    viewController = [[RootViewController alloc] initWithNibName:nil bundle:nil];
    viewController.wantsFullScreenLayout = YES;
    viewController.view = eaglView;
    //竖屏初始化
    viewControllerV = [[RootViewControllerV alloc] initWithNibName:nil bundle:nil];
    viewControllerV.wantsFullScreenLayout = YES;
   
    
    // Set RootViewController to window
    if ( [[UIDevice currentDevice].systemVersion floatValue] < 6.0)
    {
        // warning: addSubView doesn't work on iOS6
        [window addSubview: viewController.view];
    }
    else
    {
        // use this method on ios6
        [window setRootViewController:viewController];
    }

新增切换函数changedActivityOrientation

+(void)changedActivityOrientation:(NSDictionary*) dict
{
    NSObject* obj = [dict objectForKey:@"type"];
    if (nil != obj)
    {
        int type = [obj intValue];
        AppController * pApp = (AppController*)[[UIApplication sharedApplication] delegate];
        CCEAGLView* glView = pApp->eaglView;
        if (type == 1)//横屏
        {
            pApp->viewControllerV.view = nil;
            pApp->viewController.view = glView;
            if ([[UIDevice currentDevice].systemVersion floatValue] < 6.0)
            {
                [pApp->window addSubview:pApp->viewController.view];
            }
            else
            {
                [pApp->window setRootViewController:pApp->viewController];
            }
        }
        else    //竖屏
        {
            pApp->viewController.view = nil;
            pApp->viewControllerV.view = glView;
            if ([[UIDevice currentDevice].systemVersion floatValue] < 6.0)
            {
                [pApp->window addSubview:pApp->viewControllerV.view];
            }
            else
            {
                [pApp->window setRootViewController:pApp->viewControllerV];
            }
            
        }
        [pApp->window makeKeyAndVisible];
    }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值