I'd like to create random images falling from the top of the screen, down to the bottom where they disappear. So far, I've been able to make 1 image fall down, at a fixed spawn point (center), but I'm unsure how to generate many more that spawn at random places on the top (somewhat like a snowfall or rainfall effect).
This is what I have so far:
-(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); }
Say suppose you have 10 images in an array, then to select any 10 images at random you would do;
int randomIndex rand()/RAND_MAX * 10 + 1;
Then in some method, you could animate it as;
-(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];
}
Your NSTimer should trigger this method and then this would select the random image out of your images, here I suppose 10 images that you have in an array. Then this would animate the images from outside the top/right corner to the bottom left corner at the specified time interval as specified with NSTimer.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.