I'm creating a program where an image of a bird continuously falls from the top of the screen (like "raining" birds). In order to have an NSTimer for each individual bird, I made a UIImageView subclass (called "BirdUIImageView"). However, I'm not sure how to implement the code correctly--where to put what, etc.
Here is the code I have in ViewController.m:
#import "ViewController.h"
#import "BirdUIImageView.h"
@interface ViewController ()
@end
@implementation ViewController {
BirdUIImageView *_myImage;
}
- (void)viewDidLoad
{
//IMAGE CREATOR TIMER
createImagesTimer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(createImages) userInfo:nil repeats:YES];
}
//CREATES AN IMAGE
-(void) createImages {
srand(time(NULL));
int random_x_coordinate = rand() % 286;
CGRect myImageRect = CGRectMake(random_x_coordinate, 0.0f, 40.0f, 40.0f);
BirdUIImageView *myImage = [[BirdUIImageView alloc] initWithFrame:myImageRect];
[myImage setImage:[UIImage imageNamed:@"flake.png"]];
myImage.opaque = YES;
[self.view addSubview:myImage];
_myImage = myImage;
}
And here is the code I have in BirdUIImageView.m. I'm totally at a loss as to what to do in this file, but I made an attempt:
#import "BirdUIImageView.h"
@implementation BirdUIImageView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
- (void)viewDidLoad
{
//FALLING BIRDS TIMER
moveObjectTimer = [NSTimer scheduledTimerWithTimeInterval:0.001 target:self selector:@selector(moveObject) userInfo:nil repeats:YES];
}
//FALLING BIRDS MOVER
-(void) moveObject {
_myImage.center = CGPointMake(_myImage.center.x, _myImage.center.y +1);
}