最近几天在研究游戏适配IphoneX机型的问题,现总结一下以防忘记,之前想着通过采用网上一篇介绍cocos2dx 适配X的方法实现全屏,
链接:https://mp.weixin.qq.com/s/2icxj1E8t2540Zr0j4aIZA,
https://github.com/biganans/cocos2x-adaptation
这样是解决了全屏的问题,但是因为齐刘海的问题,有些按钮都被遮挡住了,如果单独对这些遮挡的按钮进行位置操作的话,整个ui布局效果就会很差,而且针对每一个被遮挡的控件界面都要操作,工作量还是比较大的,我们游戏也已经开发了一些时日,所以在这个基础上进行了一些修改,采用两边留出一部分用图片填充,游戏UI都在中间部分展示的方式
首先,在 RootViewController.mm文件里添加方法“viewSafeAreaInsetsDidChange”,这个方法是干嘛的在xcode Quick help里都有介绍,这里就不说了,:
- (void)viewSafeAreaInsetsDidChange {
[superviewSafeAreaInsetsDidChange];
NSLog(@"viewSafeAreaInsetsDidChange %@",NSStringFromUIEdgeInsets(self.view.safeAreaInsets));
[selfupdateOrientation];
}
bool changeViewFrame = false;
- (void)updateOrientation {
if (@available(iOS11.0, *)) {
CGRect rect = [[UIScreenmainScreen]bounds];
CGSize size = rect.size;
CGFloat width = size.width;
CGFloat height = size.height;
CGFloat scale_screen = [UIScreenmainScreen].scale;
//通过分辨率判断是否是iPhoneX手机
if (width*scale_screen ==2436and height*scale_screen ==1125)
{
if (self.viewand !changeViewFrame)
{
CGRect s =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);
self.view.frame = s;
changeViewFrame =true;
}
}
}
}
里面做了一些判断,主要是通过分辨率来判定是否是X(这个方法可能不好,如果有谁知道更好的方法,也可以贴出来大家学习学习~~),显示区域选择中间某一块。
其次,在“AppController.mm”文件里的 “
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions” 方法里进行处理:
// Set RootViewController to window
if ( [[UIDevicecurrentDevice].systemVersionfloatValue] < 6.0)
{
// warning: addSubView doesn't work on iOS6
[windowaddSubview:_viewController.view];
}
else
{
// use this method on ios6
[windowsetRootViewController:_viewController];
}
[windowmakeKeyAndVisible];
[[UIApplicationsharedApplication]setStatusBarHidden:true];
if (@available(iOS11.0, *))
{
CGRect rect = [[UIScreenmainScreen]bounds];
CGSize size = rect.size;
CGFloat width = size.width;
CGFloat height = size.height;
CGFloat scale_screen = [UIScreenmainScreen].scale;
//通过分辨率判断是否是iPhoneX手机
if (width*scale_screen ==2436and height*scale_screen ==1125)
{
UIView *viewLeft = [[UIViewalloc]initWithFrame:CGRectMake(0,0,31,_viewController.view.frame.size.height)];
UIImageView* imageViewL = [[UIImageViewalloc]initWithFrame:viewLeft.bounds];
imageViewL.image = [UIImageimageNamed:@"bg_back.png"];
[viewLeft addSubview:imageViewL];
[window addSubview:viewLeft];
[window sendSubviewToBack:viewLeft];
UIView *viewRight = [[UIViewalloc]initWithFrame:CGRectMake(_viewController.view.frame.size.width + 31,0,31,_viewController.view.frame.size.height)];
UIImageView* imageView = [[UIImageViewalloc]initWithFrame:viewRight.bounds];
imageView.image = [UIImageimageNamed:@"bg_back.png"];
[viewRight addSubview:imageView];
[window addSubview:viewRight];
[window sendSubviewToBack:viewRight];
}
}
// IMPORTANT: Setting the GLView should be done after creating the RootViewController
cocos2d::GLView *glview =cocos2d::GLViewImpl::createWithEAGLView((__bridgevoid *)_viewController.view);
cocos2d::Director::getInstance()->setOpenGLView(glview);
//run the cocos2d-x game scene
app->run();
return YES;
这样操作之后UI都显示在中间区域部分了,但是后来在运行测试的时候,又发现了一个bug,就是在输入框输入东西的时候,整个视图做移动缩放动画,位置更改了,通过debug 之后发现在文件“CCEAGLView-ios.mm” 下图这个方法里添加额外处理:
- (id) initWithFrame:(CGRect)frame pixelFormat:(NSString*)format depthFormat:(GLuint)depth preserveBackbuffer:(BOOL)retained sharegroup:(EAGLSharegroup*)sharegroup multiSampling:(BOOL)sampling numberOfSamples:(unsigned int)nSamples
{
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;
//通过分辨率判断是否是iPhoneX手机
if (width*scale_screen == 2436 and height*scale_screen == 1125)
{
frame = CGRectMake(31,0,750,375);
}
}
//其他的操作
}
这样的话就行了,效果图就不上传了,目前还没有IphoneX机子,这个是在模拟器上实现的,如果哪位同学有更好的方法,可以相互学习学习~~~~~~~~~