我想在使用kobold2d库的应用程序中除了UIKit图形用户界面元素之外还包括cocos2d场景,所以它应该只占屏幕的一部分。
在初始化第一层时,我像这样改变了cocos2d的glview大小:
UIView * glView = [[CCDirector sharedDirector]view];
CGRect rct = CGRectMake(100, 100, 300, 400);
[glView setFrame:rct];视图显示正常,直到我更改了方向,然后glview再次变成全屏。有没有办法禁用这种行为?
XCode 4.5.2、iOS 6、Kobold2D 2.04
发布于 2013-01-23 20:04:19
在设置框架后调用重塑可能会有所帮助:
UIView * glView = [[CCDirector sharedDirector]view];
CGRect rct = CGRectMake(100, 100, 300, 400);
[glView setFrame:rct];
[glView reshape];发布于 2013-01-24 21:57:50
现在经过一些实验,我找到了解决方案,如果我错了,请纠正我。至少这对我是有效的,希望其他人也能使用它。
CCDirectorIOS是一个UIViewController,通常被设置为app window的rootViewController。
看起来每次方向改变时,[window rootViewController]都要负责使它的view全屏,从而迫使所有的孩子进行布局(我知道!%)。
因此,为了避免glView时不时地缩小到整个窗口,应将另一个UIViewController作为rootController,而不是CCDirector。另一个UIViewController应该有它自己的全新UIView。这个新的rootView是这个rootViewController可以随心所欲地调整大小的东西,别管我们的glView了。现在,将director指定为rootController的子项,并将glView指定为rootView的子项。
将glView设置为您想要的大小--开始吧!现在当rootView调整大小时,glView就不会了,在我的例子中,它仍然需要在保持自由空间比例的同时调整大小,所以我在它上面使用了setAutoresizingMask。请大师告诉我你的想法。
已知的警告:
除了正确的初始orientation
,最低ios版本的门槛提高到5
app的AppDelegate中的代码(假设ARC开启):
-(void) initializationComplete
{
#ifdef KK_PLATFORM_IOS
RootViewController * rootvc = [[RootViewController alloc]init];
UIView * rootView = [[UIView alloc]init];
[rootvc setView:rootView];
[rootvc.view setBackgroundColor:[UIColor blueColor]];//to see views in colors
[window setRootViewController:rootvc];
[rootvc addChildViewController:[CCDirector sharedDirector]];
UIView * glview = [[CCDirector sharedDirector]view];
[rootvc.view addSubview:glview];//[[CCDirector sharedDirector]openGLView]];
CGRect glRect = CGRectInset(rootvc.view.bounds, 50, 50);//our glview frame is smaller than rootView frame
[glview setFrame:glRect];
[glview setAutoresizingMask: UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth];//so the view size follows window's size even though it's not fullscreen
#endif
}https://stackoverflow.com/questions/14477901
复制相似问题