12

谁能建议一些在 cocos2d 中使用 UITextField 的链接。我想按下标签,然后UITextField应该被选中,我需要对其进行编辑UITextField

4

4 回答 4

15

我在当前项目中这样做是为了允许输入要开始播放的关卡编号,这就是为什么我的变量和方法以它们的方式命名的原因;您可能应该调整这些以对您有意义。

在您的应用控制器中,将其定义为实例变量:

  UITextField *levelEntryTextField;

在 applicationDidFinishLaunching 中创建它:

  levelEntryTextField = [[UITextField alloc] initWithFrame:
                                              CGRectMake(60, 165, 200, 90)];
  [levelEntryTextField setDelegate:self];

定义激活文本字段的方法。您还应该在应用控制器的头文件中声明它。

- (void)specifyStartLevel
{
    [levelEntryTextField setText:@""];
    [window addSubview:levelEntryTextField];
    [levelEntryTextField becomeFirstResponder];    
}

这将使在键盘上按“返回”结束编辑

- (BOOL)textFieldShouldReturn:(UITextField*)textField {
  //Terminate editing
  [textField resignFirstResponder];
  return YES;
}

这是在实际完成编辑时触发的。

- (void)textFieldDidEndEditing:(UITextField*)textField {
    if (textField==levelEntryTextField) {
        [levelEntryTextField endEditing:YES];
        [levelEntryTextField removeFromSuperview];
        // here is where you should do something with the data they entered
        NSString *result = levelEntryTextField.text;
    }
}

现在要真正启动事物,你把它放在某个地方。我从我的一个场景类中调用它,以响应用户操作:

  [[[UIApplication sharedApplication] delegate] specifyStartLevel];
于 2009-04-07T10:44:21.820 回答
10

我采用 Jack 提供的示例并实际创建了一个工作项目,这是使用 Cocos2D 0.7.1 XCode 模板完成的,然后仅编辑 *AppDelegate.m/.h 文件,这些文件在下面完整提供。我还修改了 Jack 所说的一些内容,因为我觉得在 appDidFinishLoading 中创建 UITextField 会占用太多内存,特别是如果不是一直使用文本字段...这个解决方案仅在它创建文本字段时需要时,示例绘制一个空的 Cocos2D 图层场景,并在屏幕触摸上显示文本字段供您开始输入文本。它会吐出您输入到控制台的结果 - 您可以将其传递给您自己的代码中所需的任何内容。

.h

#import <UIKit/UIKit.h>
#import "cocos2d.h"
@interface MYSCENE : Layer <UITextFieldDelegate>
{
    UITextField *myText;
}
-(void)specificStartLevel;
@end
@interface textFieldTestAppDelegate : NSObject <UIAccelerometerDelegate, UIAlertViewDelegate, UITextFieldDelegate, UIApplicationDelegate>
{
    UIWindow *window;
}
@end

然后是.m

#import "textFieldTestAppDelegate.h"
@implementation MYSCENE
-(id) init
{
    self = [super init];
    isTouchEnabled = YES;
    return self;
}
-(BOOL)ccTouchesBegan:(NSSet  *)touches withEvent:(UIEvent *)event {
    [self specifyStartLevel];
    return kEventHandled;
}
-(void)specifyStartLevel {
    myText = [[UITextField alloc] initWithFrame:CGRectMake(60, 165, 200, 90)];
    [myText setDelegate:self];
    [myText setText:@""];
    [myText setTextColor: [UIColor colorWithRed:255 green:255 blue:255 alpha:1.0]];
    [[[[Director sharedDirector] openGLView] window] addSubview:myText];
    [myText becomeFirstResponder];
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
    [myText resignFirstResponder];
    return YES;
}
-(void)textFieldDidEndEditing: (UITextField *)textField {
    if(textField == myText) {
        [myText endEditing:YES];
        [myText removeFromSuperview];
        NSString *result = myText.text;
        NSLog([NSString stringWithFormat:@"entered: %@", result]);
    } else {
        NSLog(@"textField did not match myText");
    }
}
-(void) dealloc
{
[super dealloc];
}
@end
@implementation textFieldTestAppDelegate
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
    window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [window setUserInteractionEnabled:YES];
    [[Director sharedDirector] setDisplayFPS:YES];
    [[Director sharedDirector] attachInWindow:window];
    Scene *scene = [Scene node];
    [scene addChild: [MYSCENE node]];
    [window makeKeyAndVisible];
    [[Director sharedDirector] runWithScene: scene];
}
-(void)dealloc
{
    [super dealloc];
}
-(void) applicationWillResignActive:(UIApplication *)application
{
    [[Director sharedDirector] pause];
}
-(void) applicationDidBecomeActive:(UIApplication *)application
{
    [[Director sharedDirector] resume];
}
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application
{
    [[TextureMgr sharedTextureMgr] removeAllTextures];
}
@end
于 2009-04-14T16:03:51.000 回答
2

在 cocos2d 中添加文本字段如下代码

首先,您在 Scene 中添加视图,然后在添加文本字段后添加视图,这非常容易。

-(id) init
{ 
   if( (self=[super init]) )
    {

       // add view in scene

        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 568)];
        view.backgroundColor  = [UIColor redColor];

     // add textfield in view

        UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 140, 300, 30)];
        textField.borderStyle = UITextBorderStyleRoundedRect;
        textField.font = [UIFont systemFontOfSize:15];
        textField.placeholder = @"enter text";
        textField.autocorrectionType = UITextAutocorrectionTypeNo;
        textField.keyboardType = UIKeyboardTypeDefault;
        textField.returnKeyType = UIReturnKeyDone;
        textField.clearButtonMode = UITextFieldViewModeWhileEditing;
        textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
        textField.delegate = self;
        [view addSubview:textField];

   // add view in scene

      [[[CCDirector sharedDirector] view] addSubview:view];
}
    return self;
}

你也可以在 cocos2d 中使用 CCTextfield 最好的例子是https://github.com/iNinja/CCTextField

于 2013-12-30T06:14:40.663 回答
0

尝试下面的 CCNode 子类 CCMenuItemTextField 来使用 cocos2d 中的文本字段。

该类是 CCMenuItemSprite 的直接子类。点击时,会调用“selected”方法并将 UITextField 添加到主窗口。编辑完成后,将调用“未选择”方法并将 UITextField 从屏幕中删除。用户的输入被保存到 CCLabelTTF 节点,该节点将自身定位为与原始 UITextField 完全相同。

CCMenuItemTextField.h

@interface CCMenuItemTextField : CCMenuItemSprite<UITextFieldDelegate> {
    UITextField     *textField_;
    CCLabelTTF      *label_;

    CGFloat         paddingLeft_;
}

@property (readonly, nonatomic) UITextField     *textField;
@property (readonly, nonatomic) CCLabelTTF      *label;
@property (assign, nonatomic)   CGFloat         paddingLeft;

- (void)selected;
- (void)unselected;
- (void)setFontSize:(CGFloat)size;

- (NSString*)text;
- (void)setText:(NSString*)text;

@end

CCMenuItemTextField.m

#import "CCMenuItemTextField.h"

@implementation CCMenuItemTextField

@synthesize
textField = textField_,
label = label_,
paddingLeft = paddingLeft_;

- (id)init
{
    CCSprite *normalSprite = [CCSprite spriteWithFile:@"text_field_background.png"];
    CCSprite *selectedSprite = [CCSprite spriteWithFile:@"text_field_background.png"];
    CCSprite *disabledSprite = [CCSprite spriteWithFile:@"text_field_background.png"];

    return [self initWithNormalSprite:normalSprite selectedSprite:selectedSprite disabledSprite:disabledSprite];
}

- (id)initWithNormalSprite:(CCNode<CCRGBAProtocol> *)normalSprite
            selectedSprite:(CCNode<CCRGBAProtocol> *)selectedSprite
            disabledSprite:(CCNode<CCRGBAProtocol> *)disabledSprite 
{
    self = [super initWithNormalSprite:normalSprite
                        selectedSprite:selectedSprite
                        disabledSprite:disabledSprite
                                target:self
                              selector:@selector(selected)];

    if (self) {
        paddingLeft_ = 3.0;

        textField_ = [[UITextField alloc] init];
        [textField_ setTextColor:[UIColor blackColor]];
        [textField_ setFont:[UIFont systemFontOfSize:18]];

        label_ = [[CCLabelTTF node] retain];
        [label_ setAnchorPoint:ccp(0,0.5)];
        [label_ setFontSize:18];
        [label_ setVisible:NO];
        [label_ setColor:ccBLACK];
        [self addChild:label_];
    }

    return self;
}

- (void)dealloc
{
    [label_ release];
    [textField_ release];
    [super dealloc];
}

// --------------------------------
// Public
// --------------------------------

- (void)selected
{
    [super selected];

    [label_ setVisible:NO];

    CGAffineTransform transform = [self nodeToWorldTransform];
    float textFieldHeight = textField_.font.lineHeight;
    float width = self.contentSize.width;
    float height = self.contentSize.height;
    float left = transform.tx + paddingLeft_;
    float top = 480 - transform.ty - height + (height - textFieldHeight) / 2;

    [textField_ setFrame:CGRectMake(left, top, width, height)];
    [[[[CCDirector sharedDirector] view] window] addSubview:textField_];
    [textField_ becomeFirstResponder];
    [textField_ setDelegate:self];
}

- (void)unselected
{
    [super unselected];

    [label_ setVisible:YES];
    [label_ setPosition:ccp(paddingLeft_, self.contentSize.height/2)];

    NSString *text = textField_.text ? textField_.text : @"";
    [label_ setString:text];

    [textField_ resignFirstResponder];
    [textField_ removeFromSuperview];
}

- (NSString*)text
{
    return [label_ string];
}

- (void)setText:(NSString*)text
{
    [label_ setString:text];
    [textField_ setText:text];
}

// --------------------------------
// UITextFieldDelegate
// --------------------------------

- (BOOL)textFieldShouldReturn:(UITextField*)textField {
    [self unselected];
    return YES;
}

- (void)textFieldDidEndEditing:(UITextField*)textField {
    [self unselected];
}

- (void)setFontSize:(CGFloat)size
{
    [label_ setFontSize:size];
    [textField_ setFont:[UIFont systemFontOfSize:size]];
}

// --------------------------------
// CCNode
// --------------------------------

- (void)onExitTransitionDidStart
{
    [super onExitTransitionDidStart];
    [self unselected];
}

@end
于 2012-07-17T07:48:13.190 回答