创建扫过屏幕的图像

Creating image which sweeps across the screen

我正在尝试创建横扫当前场景的横幅。我想创建一个横扫屏幕以显示当前回合的横幅。我的尝试是创建一个 UIImageView 并将其添加到当前视图。但是,我假设它调用 didMoveToView 函数并重置该场景中的所有内容,这是我不希望它做的事情。这是我的尝试:

-(void)createBanner{
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Banner"]];
    [imageView setFrame:CGRectMake(0,0, imageView.frame.size.width, imageView.frame.size.height)];
    [imageView setClipsToBounds:YES];
    [self.view addSubview:imageView];

    CABasicAnimation *sweep = [CABasicAnimation animationWithKeyPath:@"position"];
    sweep.fromValue = [NSValue valueWithCGPoint:CGPointZero];
    sweep.toValue = [NSValue valueWithCGPoint:CGPointMake(0.0, self.frame.size.height)];
    sweep.duration = 10;
    sweep.additive = YES;
    [imageView.layer addAnimation:sweep forKey:@"sweep"];

}

编辑:我正在使用 sprite kit 来创建游戏。

正如 hamobi 所说,最好在 Sprite Kit 而不是 UIKit 中使用 'SKSpriteNode'。假设你添加到 'SKScene',你上面翻译成 Sprite Kit 的代码是:

-(void)createBanner{
    SKSpriteNode* spriteNode = [SKSpriteNode spriteNodeWithImageNamed:@"Banner"]
    //It's good practice not to resize the sprite in code as it should already be the right size but...
    spriteNode.size = CGSizeMake(self.size.width, self.size.height)
    //Set its center off to the left of the screen for horizontal sweep, or you can do vertical and set it off the top of the screen...
    spriteNode.postion = CGPointMake(-spriteNode.size.width/2, self.size.height/2)
    self.addChild(spriteNode)

    //Then to sweep from left to right...
    SKAction* sweep = [SKAction moveTo:CGPointMake(spriteNode.size.width/2, self.size.height/2) duration:10]
    spriteNode.runAction(sweep)
}

我认为这涵盖了大部分内容。