Insert UIKid Component into Cocos2D's View

Some other discussion: http://www.cocoachina.com/bbs/read.php?tid=49419

Cocos2d-x UIView和Coco2d-x场景之间的相互跳转切换 http://blog.csdn.net/crayondeng/article/details/16828555

http://bit6211.iteye.com/blog/1932057

http://www.cocos2d-iphone.org/forums/topic/cocos2d-inside-uiview/

http://www.himigame.com/iphone-cocos2d/455.html

//************************************************************************************

ocos2d是使用继承于ccnode的结点类型的层。但是我想用一个opengl来绘制,就简单的情况来说必须得加一个uiview。现转载如下:

第一部分::

使用Cocos2D开发游戏和应用程序的时候,时常有些功能用系统控键很容易就实现,而cocos2d很麻烦,这时候就需要在cocos2D的程序中添加UIView或者UIView的子类。如果需要响应重力感应来支持旋转,可以向cocos2d程序中添加UIViewController的子类。

方法很简单:

[[[CCDirector sharedDirector] openGLView] addSubview:[UIView view]];

 

扫描二维码关注公众号,回复: 607074 查看本文章

第二部分,转自人人,目前看不太懂,以后应该用的到

Cocos2d中对UIView的使用

Cocos2d中想使用UIView等UIKit系的控件,最常用常见的方法,就是通过openGLView来做,虽然这个非常简单,还是简述下吧。

比如,现在我想在cocos2d中使用UIImageView这个控件,非常简单,直接上代码。

 

CGRect frame = [[[CCDirector sharedDirector] openGLView] frame];
_animateImageView = [[UIImageView alloc] initWithFrame:frame];
[[[CCDirector sharedDirector] openGLView] addSubview:_animateImageView];

 

不用了的时候。

 

[_animateImageView release];
[_animateImageView removeFromSuperview];

 

诺,这样呢,有一个问题,使用的UIImageView若不remove掉的话总是显示在最上面,再加个Sprite啥的也加不上去。恰好,这次需要的图比较大(320*480),还想借助UIImageView的动画功能,这下郁闷了。

怎么样才能即能让他顺利展示又能再上面添加Sprite或者CCMenu等呢。

 

OK, 直接给出解决方案吧。因为一旦采用openGLView addSubView的话肯定是不行的,那一个解决思路是在openGLView下插入一层View, 同时把openGLView设置成透明的,这样不就行了。

代码如下,首先要动的是AppDelegate,在openGLView下加入一层。

 

        //Set glView by wordsworth Mar.26             
        [glView setMultipleTouchEnabled:YES];
glView.opaque = NO;
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
                
                
        //add a view by wordsworth Mar.26, in order to insert another view in MainBoardLayer            
        overView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
overView.opaque = NO;
overView.backgroundColor = [UIColor clearColor];
[overView addSubview:glView];
            
[window addSubview:overView];

 

最后,把我们的ImageView尽情的写入openGLView下面吧,这样在上面加上各种各样的CCSprite、CCMenu等不用担心不显示啦。

 

AppDelegate * delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[delegate.overView insertSubview:_animateImageView belowSubview:[[CCDirector sharedDirector] openGLView]];

 

其他的UIKit控件也差不多可以按这个套路来了。

猜你喜欢

转载自shappy1978.iteye.com/blog/2010358