Reputation: 2196
How can I programmatically spawn an unlimited number of UIImageViews
on to the screen in a random location on a time increment. The random location would be:CGPoint(arc4random() % (481), arc4random() % (321),20,20)
I just don't understand how to constantly create new ones with one image variable and a for loop. Or an NSTimer
that on every increment adds a new image to the screen.
I know there are some tutorials out there for this, but the only ones I could find used automatic reference counting. It should be simple to create, and a link is fine.
Other questions:
UI is for user-interface, but since i'm not using a storyboard or xib, would there be a better type of imageView
to use.
Upvotes: 0
Views: 271
Reputation: 104082
How about something like this, in your view controller class (substituting your own image file name of course):
- (void)viewDidLoad {
[super viewDidLoad];
[NSTimer scheduledTimerWithTimeInterval:.5 target:self selector:@selector(addViews:) userInfo:nil repeats:YES];
}
-(void)addViews: (NSTimer *) aTimer {
UIImageView *iv = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"New_PICT0011.jpg"]];
CGRect rect = CGRectMake(arc4random() % (481), arc4random() % (321), 20, 20);
[iv setFrame:rect];
[self.view addSubview:iv];
}
You would have to put some kind of counter in there, and when it got to the total number of views you wanted, call [aTimer invalidate];
Upvotes: 1