I'm trying to understand the pros and cons of using something CCUIViewWrapper in Cocos2d versus a ported functionality. For instance, would it be better to use a UITableView in a CCUIViewWrapper, or to use the CCTableViewSuite. At first glance, I would assume the wrapper is the better approach since presumably it allows me to do everything the UITableView offers, but are there key details I'm missing? Are there severe limitations that exist with the wrapper either with actually using the apple sdk object or with not being able to take 开发者_开发知识库advantage of certain features within Cocos2d that come with a ported object like CCTableView?
I have copied this post from here that may help answer your question. I believe that it would be better to uses cocos2d functions with cocos2d wrappers but you can implement with others it just doesn't integrate as well.
If there are any newbies (like myself) out there that need extra help how to integrate a UIKit item with cocos2d, this might help. As you have read in the first post of this thread Blue Ether has written a wrapper for manipulating UIViews. What is a UIView? Look at http://developer.apple.com/iphone/library/documentation/uikit/reference/uikit_framework/Introduction/Introduction.html, scroll down a little bit and you will see a diagram of different UIView items; things like UIButton, UISlider, UITextView, UILabel, UIAlertView, UITableView, UIWindow and so on. There are more then 20 of them. You can use Blue Ethas code to wrap any of them inside cocos2d. Here I will wrap a UIButton, but experiment with your favorite choice of UIView item.
Create a new cocos 2d Application project.
Make a new NSObject file and call it CCUIViewWrapper. That will give you the files CCUIViewWrapper.h and CCUIViewWrapper.m. Edit (by copy paste) them so they look as Blue Ether has defined them (see the first post in this thread.
Make your HelloWorld.h look like this
// HelloWorldLayer.h #import "cocos2d.h" #import <UIKit/UIKit.h> #import "CCUIViewWrapper.h" @interface HelloWorld : CCLayer { UIButton *button; CCUIViewWrapper *wrapper; } +(id) scene; @end
and your HelloWorld.m like this
// HelloWorldLayer.m
#import "HelloWorldScene.h"
// HelloWorld implementation
@implementation HelloWorld
+(id) scene
{
CCScene *scene = [CCScene node];
HelloWorld *layer = [HelloWorld node];
[scene addChild: layer];
return scene;
}
-(void)buttonTapped:(id)sender
{
NSLog(@"buttonTapped");
}
// create and initialize a UIView item with the wrapper
-(void)addUIViewItem
{
// create item programatically
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchDown];
[button setTitle:@"Touch Me" forState:UIControlStateNormal];
button.frame = CGRectMake(0.0, 0.0, 120.0, 40.0);
// put a wrappar around it
wrapper = [CCUIViewWrapper wrapperForUIView:button];
[self addChild:wrapper];
}
-(id) init
{
if( (self=[super init] )) {
[self addUIViewItem];
// x=160=100+120/2, y=240=260-40/2
wrapper.position = ccp(100,260);
[wrapper runAction:[CCRotateTo actionWithDuration:4.5 angle:180]];
}
return self;
}
- (void) dealloc
{
[self removeChild:wrapper cleanup:true];
wrapper = nil;
[button release];
button=nil;
[super dealloc];
}
@end
Build and Run. This should produce a rotating button in the middle of the scene. You can touch the button while it rotates. It will write a text message in the Console.
CCUIViewWrapper is not a good solution. I have tried and removed it. The problem is that it wraps the UIKit element in a container and adds it as a view on director over the openGL main view. One problem with that is that you will not be able to add any other sprite on top of it. Very limited.
精彩评论