uikit - 使用 CCUIViewWrapper 与移植功能的优缺点

标签 uikit cocos2d-iphone tradeoff

我试图了解在 Cocos2d 中使用 CCUIViewWrapper 与移植功能的优缺点。例如,在 CCUIViewWrapper 中使用 UITableView 还是使用 CCTableViewSuite 会更好。乍一看,我认为包装器是更好的方法,因为大概它允许我做 UITableView 提供的所有事情,但是有没有我遗漏的关键细节?包装器是否存在严重的限制,要么实际使用苹果 sdk 对象,要么无法利用 Cocos2d 中的某些功能,这些功能随移植对象(如 CCTableView)一起提供?

最佳答案

我从 here 复制了这篇文章这可能有助于回答您的问题。我相信将 cocos2d 函数与 cocos2d 包装器一起使用会更好,但您可以与其他人一起实现它只是不集成。

如果有任何新手(比如我自己)需要额外的帮助来如何将 UIKit 项与 cocos2d 集成,这可能会有所帮助。正如您在本主题的第一篇文章中所读到的,Blue Ether 编写了一个用于操作 UIViews 的包装器。什么是 UIView?看 http://developer.apple.com/iphone/library/documentation/uikit/reference/uikit_framework/Introduction/Introduction.html ,向下滚动一点,您将看到不同 UIView 项的图表; UIButton、UISlider、UITextView、UILabel、UIAlertView、UITableView、UIWindow 等。其中有20多个。您可以使用 Blue Ethas 代码将它们中的任何一个包装到 cocos2d 中。在这里,我将包装一个 UIButton,但尝试使用您最喜欢的 UIView 项目选择。

  • 创建一个新的 cocos 2d 应用程序项目。
  • 创建一个新的 NSObject 文件并将其命名为 CCUIViewWrapper。这将为您提供文件 CCUIViewWrapper.h 和 CCUIViewWrapper.m。编辑(通过复制粘贴)它们,使它们看起来像 Blue Ether 定义的那样(请参阅此线程中的第一篇文章。
  • 让你的 HelloWorld.h 看起来像这样
    //  HelloWorldLayer.h
    
    #import "cocos2d.h"
    
    #import <UIKit/UIKit.h>
    
    #import "CCUIViewWrapper.h"
    
    @interface HelloWorld : CCLayer
    
    {
        UIButton *button;
        CCUIViewWrapper *wrapper;
    }
    +(id) scene;
    @end
    

  • 和你的 HelloWorld.m 像这样
    //  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
    

    构建和运行。这应该会在场景中间产生一个旋转按钮。您可以在旋转时触摸该按钮。它将在控制台中写入一条文本消息。

    关于uikit - 使用 CCUIViewWrapper 与移植功能的优缺点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3836415/

    相关文章:

    ios - 在保持背景动态的同时向 View 添加动画模糊

    ios - 从 UITabBarController 内部呈现 UINavigationController

    iphone - 发送动态数据

    iphone - 无法从 C++ 文件访问对象属性 (Cocos2D/Box2d)

    python - Python3中合并k个排序列表,内存和时间之间的权衡问题

    ios - 如何通过UIkit将组件嵌套到组件?

    ios - 生成图像的 UIImageView 性能问题

    iphone - Apple 目前不接受使用此版本 SDK 构建的应用程序

    数据库替代品?