解除在场景中以编程方式添加为子视图的文本视图

时间:2015-05-06 20:15:06

标签: ios objective-c sprite-kit uitextview skscene

正如标题所示,我已经在我的场景中添加了一个UITextView,它运行正常。我唯一的问题是,当我使用后退按钮返回主菜单时,文本视图仍保留在屏幕上。对于我的生活,如果按下后退按钮,我无法弄清楚如何防止此文本视图保留在屏幕上。请有人帮助我。以下是我在SKScene中使用的代码:

@implementation HowToPlay


-(void)didMoveToView:(SKView *)view {

SKSpriteNode *bgImage = [SKSpriteNode spriteNodeWithImageNamed:@"vortex1.jpg"];
bgImage.size = self.frame.size;
bgImage.position = CGPointMake(self.size.width/2, self.size.height/2);
[self addChild:bgImage];

SKLabelNode *myLabel = [SKLabelNode labelNodeWithFontNamed:@"Thonburi-Bold"];
myLabel.text = @"Title";
myLabel.fontSize = 20;
myLabel.fontColor = [UIColor whiteColor];
myLabel.position = CGPointMake(275,290);
[self addChild:myLabel];

UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(150, 40, 300, 240)];
textView.backgroundColor = [ UIColor  clearColor];
textView.text = @"this is the body of the text that we are going to use and add the story line to it.this is the body of the text that we are going to use and add the story line to it.\n\nthis is the body of the text that we are going to use and add the story line to it.this is the body of the text that we are going to use and add the story line to it.this is the body of the text that we are going to use and add the story line to it.\n\nthis is the body of the text that we are going to use and add the story line to it.this is the body of the text that we are going to use and add the story line to it.";
textView.font = [UIFont fontWithName:@"Helvetica" size:20];
textView.textColor = [UIColor whiteColor];
[self.view addSubview:textView];

[self addChild: [self backButton]];
}
- (SKSpriteNode *)backButton
{
SKSpriteNode *button = [SKSpriteNode spriteNodeWithImageNamed:@"Back.png"];
[button setScale:0.5];
button.position = CGPointMake(50, 50);
button.name = @"Back";
return button;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
SKNode *node = [self nodeAtPoint:location];

if ([node.name isEqualToString:@"Back"]) {
    [self runButtonActionOnNodeBack:node];
}
}

-(void)runButtonActionOnNodeBack:(SKNode*) node{
node.name = nil;
//SKAction *moveUp = [SKAction moveByX: 0 y: 100.0 duration: 0.5];
SKAction *zoom = [SKAction scaleTo: 0.8 duration: 0.25];
// *spin = [SKAction rotateToAngle:360.0f duration:1.0];
SKAction *pause = [SKAction waitForDuration: 0.5];
SKAction *fadeAway = [SKAction fadeOutWithDuration: 0.25];
SKAction *remove = [SKAction removeFromParent];
SKAction *moveSequence = [SKAction sequence:@[zoom, pause, fadeAway, remove]];
[node runAction: moveSequence completion:^{
    SKScene *mainMenu  = [[GameScene alloc] initWithSize:self.size];
    SKTransition *doors = [SKTransition doorsOpenHorizontalWithDuration:0.5];
    [self.view presentScene:mainMenu transition:doors];
}];
}
@end

1 个答案:

答案 0 :(得分:2)

保存对该文本视图的引用,然后在想要删除它时将其从超级视图中删除:

@interface HowToPlay()
@property (nonatomic, strong) UITextView *textView;
@end

@implementation HowToPlay

- (void)didMoveToView:(SKView *)view {
    self.textView = [[UITextView alloc]......
}

//whenever you want to open the menu/remove the text view:
[self.textView removeFromSuperview];