2012-11-23 70 views
1

我想要創建隨機圖像從屏幕頂部下降到底部,然後消失。到目前爲止,我已經能夠在一個固定的重生點(中心)下降1張圖片,但我不確定如何在頂部的隨機位置產生更多的圖片(有點像降雪或降雨效果)。生成隨機下降圖像(xcode)

這是我到目前爲止有:

-(void) viewDidLoad { 
    moveObjectTimer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(moveObject) userInfo:nil repeats:YES]; } 


-(void) moveObject {  // + means down and 5 is SPEED 
    bird.center = CGPointMake(bird.center.x, bird.center.y +5); } 

回答

0

說假設你有10個圖像的數組,然後隨機選擇任何10幅圖像,你會怎麼做;

int randomIndex rand()/ RAND_MAX * 10 + 1;

然後在某些方法中,可以將它製作爲動畫;

-(void) moveObject{ 
    int randomIndex = rand()/RAND_MAX * 10 + 1; 
    UIImage *image = [self.myImages objectAtIndex:randomIndex]; 
    UIImageView *imageView = [[UIImageView alloc] initWithImage: image]; 
    CGRect mainRect = CGRectMake(view.frame.size.width + image.size.width,view.frame.size.height - image.size.height,image.frame.size.width,image.size.height); 
    imageView.frame mainRect; 
    [UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{ 
     mainRect.frame.size.origin.x = 0; 
     mainRect.frame.origin.y = 0; 
     imageView.frame =mainRect; 
    } completion:nil]; 
} 

你的NSTimer應該觸發這個方法,那麼這將選擇出你的圖像的隨機圖像,在這裏我想你在陣列中有10張圖像。然後這將按照NSTimer指定的指定時間間隔將圖像從頂部/右角外部移動到左下角。

+0

對不起,我其實只是想反覆使用相同的圖像。我想我可以用你給我的代碼解決它。非常感謝! – user1824518