2015-02-23 39 views
0

我正在嘗試創建掃描當前場景的橫幅。我想創建一個橫幅,它可以向下掃描屏幕以顯示當前輪。我試圖創建一個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套件爲了創建遊戲。

+1

你實際上是在製作一個Sprite Kit項目嗎?如果是這樣,那麼你不應該使用UIKit來做任何這樣的事情。 – hamobi 2015-02-23 17:39:47

+0

您使用'self.view'作爲超級視圖,'self.frame'作爲您的最終位置。您可能想要將其更改爲'self.view.frame'。 – 2015-02-23 17:39:53

+0

@hamobi是的,我正在使用sprite套件來做到這一點。我覺得這不是正確的做法。你在暗示什麼? – AzureWorld 2015-02-23 18:45:09

回答

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

我認爲它涵蓋了大部分。