【问题标题】:Creating image which sweeps across the screen创建横扫屏幕的图像
【发布时间】:2015-02-24 17:13:09
【问题描述】:

我正在尝试创建一个横扫当前场景的横幅。我想创建一个横扫屏幕以显示当前回合的横幅。我的尝试是创建一个 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 项目?如果是这样,那么你不应该使用 UIKit 来做这些。
  • 您使用self.view 作为超级视图,self.frame 作为您的最终位置。您可能希望将其更改为 self.view.frame
  • @hamobi 是的,我正在使用精灵套件来执行此操作。我觉得这不是正确的做法。你有什么建议?

标签: ios objective-c sprite-kit


【解决方案1】:

正如 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)
}

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-06
    • 2015-06-15
    • 2012-12-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多