如何在 SpriteKit 上创建 UITextField
Posted
技术标签:
【中文标题】如何在 SpriteKit 上创建 UITextField【英文标题】:How to create a UITextField on SpriteKit 【发布时间】:2013-10-29 12:20:54 【问题描述】:我想实现一个文本字段来在我的 SpriteKit 游戏中输入你的名字,所以我有这样的东西:
SKLabelNode* gameTitle = [SKLabelNode labelNodeWithFontNamed:@"Chalkduster"];
gameTitle.text = @"Game Title";
gameTitle.name = @"gametitle";
gameTitle.fontSize = 44.0;
gameTitle.fontColor = [SKColor blackColor];
gameTitle.position = CGPointMake(self.size.width/2,self.size.height/2 + 150);
[self addChild:gameTitle];
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(self.size.width/2, self.size.height/2+20, 200, 40)];
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.textColor = [UIColor blackColor];
textField.font = [UIFont systemFontOfSize:17.0];
textField.placeholder = @"Enter your name here";
textField.backgroundColor = [SKColor whiteColor];
textField.autocorrectionType = UITextAutocorrectionTypeYes;
textField.keyboardType = UIKeyboardTypeDefault;
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
textField.delegate = self.delegate;
[self.view addSubview:textField];
所以,问题是它没有显示在我的场景中,也许问题是在将它添加到场景中时,我使用了 addSubview。
错了吗?
【问题讨论】:
尝试使用 UITextView @LearnCocos2D 还是一样,没有出现:/ 【参考方案1】:您可以在didMoveToView:(SKView *)view
函数中添加继承 UIView 的对象
只需将该函数添加到您的 SKScene
并将您的 UITextField
移到里面:
-(void)didMoveToView:(SKView *)view
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(self.size.width/2, self.size.height/2+20, 200, 40)];
textField.center = self.view.center;
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.textColor = [UIColor blackColor];
textField.font = [UIFont systemFontOfSize:17.0];
textField.placeholder = @"Enter your name here";
textField.backgroundColor = [UIColor whiteColor];
textField.autocorrectionType = UITextAutocorrectionTypeYes;
textField.keyboardType = UIKeyboardTypeDefault;
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
textField.delegate = self.delegate;
[self.view addSubview:textField];
【讨论】:
伟大而有效的解决方案。谢谢! 感谢您的回答。如果我更改为另一个场景,则 TextField 将保留在视图中。我必须手动删除它吗? 不幸的是你会的,@palme 感谢您的澄清。 谢谢!但是怎么去掉呢??【参考方案2】:删除它使用
[myTextField removeFromSuperView];
如果你不想使用在场景演示中插入textField的方法didMoveToView,你可以使用这个方法:
-(void)insertUI
ttaAppDelegate *myDel = [[UIApplication sharedApplication] delegate];
UIViewController *vc = myDel.window.rootViewController;
self.textField= [[UITextField alloc] initWithFrame:CGRectMake(self.size.width/2, self.size.height/2+20, 200, 40)];
self.textField.center = self.view.center;
self.textField.borderStyle = UITextBorderStyleRoundedRect;
self.textField.textColor = [UIColor blackColor];
self.textField.font = [UIFont systemFontOfSize:17.0];
self.textField.placeholder = @"Enter your name here";
self.textField.backgroundColor = [UIColor whiteColor];
self.textField.autocorrectionType = UITextAutocorrectionTypeYes;
self.textField.keyboardType = UIKeyboardTypeDefault;
self.textField.clearButtonMode = UITextFieldViewModeWhileEditing;
self.textField.delegate = self;
self.button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[self.button addTarget:self action:@selector(saveScore:) forControlEvents:UIControlEventTouchDown];
[self.button setTitle:@"Save" forState:UIControlStateNormal];
self.button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[vc.view addSubview:self.textField];
[vc.view addSubview:self.button];
vc.interstitialPresentationPolicy = ADInterstitialPresentationPolicyManual;
[vc requestInterstitialAdPresentation];
【讨论】:
以上是关于如何在 SpriteKit 上创建 UITextField的主要内容,如果未能解决你的问题,请参考以下文章
如何在 spritekit 中使用页面控件? (以编程方式)
在更新函数中使用 spriteKit 增加 spriteNode 的大小(Swift)