简体   繁体   English

如何在SpriteKit中使用UITextView?

[英]How can I use a UITextView in SpriteKit?

So I have this UITextView in my game, and I want to make it more dynamic by adding SKAction animations and having it interact with physics . 所以我在游戏中有了这个UITextView ,我想通过添加SKAction动画并使它与物理交互来使其更加动态。 The former I did in kind of a hacky way which I'm not quite satisfied with, the latter I haven't done as I haven't a clue how to. 前者我种这我不是很满意,一个哈克的方式一样,后者我没有这样做,因为我还没有线索如何。

This is how I did the SKActions: 这是我执行SKAction的方法:

I made a couple of ivars: 我犯了几个错误:

- textViewContent (NSString) -textViewContent(NSString)

- textViewSize (CGRect) -textViewSize(CGRect)

- textView (UITextView) -textView(UITextView)

- fakeTextView (SKSpriteNode) -fakeTextView(SKSpriteNode)

Called by didMoveToView: 由didMoveToView调用:

-(void)createTextView {

    textViewContent = @"foo";
    textViewSize = CGRectMake(CGRectGetMidX(self.frame)-(self.frame.size.width/4),
                          CGRectGetMidY(self.frame)-(self.frame.size.height/4), self.frame.size.width/2, self.frame.size.height/2);


    textView = [[UITextView alloc]initWithFrame:textViewSize];

    textView.backgroundColor = [SKColor orangeColor];
    textView.text = textViewContent;
    textView.textColor = [SKColor whiteColor];
    textView.textAlignment = NSTextAlignmentLeft;
    textView.font = [UIFont fontWithName:@"Code-Pro-Demo" size:25];
    textView.layer.zPosition = -1;
    textView.alpha = 0;
    textView.editable = NO;


    fakeTextView = [SKSpriteNode spriteNodeWithColor:[SKColor orangeColor] size:CGSizeMake(self.frame.size.width/2, self.frame.size.height/2)];
    fakeTextView.position = CGPointMake((self.frame.size.width*1.5),CGRectGetMidY(self.frame));
    [self addChild:fakeTextView];
    [self textViewEntersScene];


}
-(void)textViewEntersScene{
    SKAction *wait = [SKAction waitForDuration:0.5];
    SKAction *moveIn = [SKAction moveToX:(self.frame.size.width/2) - 100 duration:0.3];
    SKAction *moveBackSlightly = [SKAction moveToX:self.frame.size.width/2 duration:0.2];

    SKAction *displayTextView = [SKAction runBlock:^{
        textView.alpha = 1;
}];

    SKAction *hideFakeTextView = [SKAction runBlock:^{
        fakeTextView.hidden = YES;
}];  

    [fakeTextView runAction:[SKAction sequence:@[wait,moveIn,moveBackSlightly,displayTextView,hideFakeTextView]]];


}

As you can probably tell, the possibilities are very limited. 您可能会说,可能性非常有限。 Is there a better way of achieving this or something similar? 是否有更好的方法可以实现这一目标或类似目标?

I created a class to display and scroll text. 我创建了一个用于显示和滚动文本的类。 You can do a straight copy and paste for the .h and .m files once you create a new SKNode class. 创建新的SKNode类后,可以直接复制并粘贴.h和.m文件。

Basically the class displays SKLabelNodes and scrolls them up if there is more than 1 line. 基本上,该类显示SKLabelNodes,如果多于1行,则将它们向上滚动。 Lines appear for a set time, move up if additional lines are added and then fade out. 线条会显示一段设定的时间,如果添加了其他线条,则会向上移动,然后淡出。

It's pretty generic stuff so feel free to use and modify the code in any project you want. 这是非常普通的东西,因此可以在任何您想要的项目中随意使用和修改代码。

To use the class, import the header and use this code (example): 要使用该类,请导入标题并使用以下代码(示例):

TextBubble *myBubble = [[TextBubble alloc] init];
[myBubble createBubbleWithText:@"Hello this is a very nice day to go fishing and have a cold beer." textSize:24 maxLineLength:20];
myBubble.position = CGPointMake(300, 300);
[self addChild:myBubble];

TextBubble Header File TextBubble头文件

#import <SpriteKit/SpriteKit.h>

@interface TextBubble : SKNode

-(instancetype)init;
-(void)createBubbleWithText:(NSString *)textString textSize:(int)textSize maxLineLength:(int)maxLineLength;

@end

TextBubble Implementation File TextBubble实现文件

#import "TextBubble.h"

@implementation TextBubble {
    NSMutableArray *linesArray;
}

- (instancetype)init {
    self = [super init];
    if (self) {
        linesArray = [[NSMutableArray alloc] init];
    }
    return self;
}

-(void)createBubbleWithText:(NSString *)textString textSize:(int)textSize maxLineLength:(int)maxLineLength {

    NSMutableString *passedText = [[NSMutableString alloc] initWithString:textString];
    NSMutableString *currentLine = [[NSMutableString alloc] init];
    unsigned long characterCounter = 0;
    BOOL keepGoing = true;

    while (keepGoing) {

        NSRange whiteSpaceRange = [passedText rangeOfCharacterFromSet:[NSCharacterSet whitespaceCharacterSet]];

        if(whiteSpaceRange.location != NSNotFound) {
            characterCounter += whiteSpaceRange.location+1;

            if(characterCounter <= maxLineLength) {
                [currentLine appendString:[passedText substringWithRange:NSMakeRange(0, whiteSpaceRange.location+1)]];
                [passedText setString:[passedText substringWithRange:NSMakeRange(whiteSpaceRange.location+1, [passedText length]-whiteSpaceRange.location-1)]];
            } else {
                [currentLine setString:[currentLine substringWithRange:NSMakeRange(0, [currentLine length]-1)]];

                SKLabelNode *myNode = [SKLabelNode labelNodeWithFontNamed:@"Arial-BoldMT"];
                myNode.text = [NSString stringWithString:currentLine];
                myNode.fontSize = textSize;
                myNode.fontColor = [SKColor blueColor];
                myNode.horizontalAlignmentMode = SKLabelHorizontalAlignmentModeCenter;
                myNode.verticalAlignmentMode = SKLabelVerticalAlignmentModeCenter;
                myNode.zPosition = 0;
                [linesArray addObject:myNode];

                characterCounter = 0;
                [currentLine setString:@""];
            }
        } else {
            [currentLine appendString:passedText];

            SKLabelNode *myNode = [SKLabelNode labelNodeWithFontNamed:@"Arial-BoldMT"];
            myNode.text = [NSString stringWithString:currentLine];
            myNode.fontSize = textSize;
            myNode.fontColor = [SKColor blueColor];
            myNode.horizontalAlignmentMode = SKLabelHorizontalAlignmentModeCenter;
            myNode.verticalAlignmentMode = SKLabelVerticalAlignmentModeCenter;
            myNode.zPosition = 0;
            [linesArray addObject:myNode];

            keepGoing = false;
        }
    }

    if([linesArray count] == 1) {
        SKAction *block0 = [SKAction runBlock:^{
            SKLabelNode *myNode = [linesArray objectAtIndex:0];
            myNode.alpha = 0.0;
            [self addChild:myNode];
            [myNode runAction:[SKAction fadeAlphaTo:1.0 duration:0.5]];
        }];

        SKAction *wait1 = [SKAction waitForDuration:2.5];

        SKAction *block1 = [SKAction runBlock:^{
            SKLabelNode *myNode = [linesArray objectAtIndex:0];
            [myNode runAction:[SKAction fadeAlphaTo:0.0 duration:0.5]];
        }];

        SKAction *wait2 = [SKAction waitForDuration:0.5];

        SKAction *block2 = [SKAction runBlock:^{
            [[linesArray objectAtIndex:0] removeFromParent];
        }];

        [self runAction:[SKAction sequence:@[block0, wait1, block1, wait2, block2]]];
    }

    if([linesArray count] == 2) {
        float heightCounter = 0.0;

        for (int i = 0; i<[linesArray count]; i++) {

            SKAction *wait0 = [SKAction waitForDuration:(2.0 * i)];

            SKAction *block0 = [SKAction runBlock:^{
                SKLabelNode *myNode = [linesArray objectAtIndex:i];
                myNode.alpha = 0.0;
                myNode.position = CGPointMake(0, 0);
                [self addChild:myNode];
                [[linesArray objectAtIndex:i] runAction:[SKAction fadeAlphaTo:1.0 duration:0.5]];
            }];

            SKAction *wait1 = [SKAction waitForDuration:1.5];

            heightCounter += 30;

            SKAction *block1 = [SKAction runBlock:^{
                [[linesArray objectAtIndex:i] runAction:[SKAction moveToY:heightCounter duration:0.5]];
            }];

            SKAction *block2 = [SKAction runBlock:^{
                [[linesArray objectAtIndex:i] runAction:[SKAction fadeAlphaTo:0.0 duration:0.5]];
            }];

            SKAction *wait2 = [SKAction waitForDuration:0.5];

            SKAction *block3 = [SKAction runBlock:^{
                [[linesArray objectAtIndex:i] removeFromParent];
            }];

            [self runAction:[SKAction sequence:@[wait0, block0, wait1, block1, wait1, block2, wait2, block3]]];

            heightCounter = 0;
        }
    }

    if([linesArray count] >= 3) {
        float heightCounter = 0.0;

        for (int i = 0; i<[linesArray count]; i++) {

            SKAction *wait0 = [SKAction waitForDuration:(1.5 * i)];

            SKAction *block0 = [SKAction runBlock:^{
                SKLabelNode *myNode = [linesArray objectAtIndex:i];
                myNode.alpha = 0.0;
                myNode.position = CGPointMake(0, 0);
                [self addChild:myNode];
                [[linesArray objectAtIndex:i] runAction:[SKAction fadeAlphaTo:1.0 duration:0.5]];
            }];

            SKAction *wait1 = [SKAction waitForDuration:1.5];

            heightCounter += 30;

            SKAction *block1 = [SKAction runBlock:^{
                [[linesArray objectAtIndex:i] runAction:[SKAction moveToY:heightCounter duration:0.5]];
            }];

            heightCounter += 30;

            SKAction *block5 = [SKAction runBlock:^{
                [[linesArray objectAtIndex:i] runAction:[SKAction moveToY:heightCounter duration:0.5]];
            }];

            SKAction *block2 = [SKAction runBlock:^{
                [[linesArray objectAtIndex:i] runAction:[SKAction fadeAlphaTo:0.0 duration:0.5]];
            }];

            SKAction *wait2 = [SKAction waitForDuration:0.5];

            SKAction *block3 = [SKAction runBlock:^{
                [[linesArray objectAtIndex:i] removeFromParent];
            }];

            [self runAction:[SKAction sequence:@[wait0, block0, wait1, block1, wait1, block5, wait1, block2, wait2, block3]]];

            heightCounter = 0;
        }
    }
}

@end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM