创建扫过屏幕的图像

时间:2015-02-23 17:24:27

标签: ios objective-c sprite-kit

我试图创建横扫当前场景的横幅。我想创建一个横扫屏幕以显示当前回合的横幅。我尝试创建一个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"];

}

编辑:我正在使用精灵工具包来创建游戏。

1 个答案:

答案 0 :(得分:0)

正如hamobi所说,最好在Sprite Kit中使用'SKSpriteNode'而不是UIKit。假设您添加了'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)
}

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