I want to simply have a loop so that an object continuously moves across the screen at the bottom. Here is my code it should be pretty easy to understand.
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self performSelector:@selector(spawnRocket) withObject:self afterDelay:2]; //delay before the object moves
}
-(void)spawnRocket{
UIImageView *rocket=[[UIImageView alloc]initWithFrame:CGRectMake(-25, 528, 25, 40)]; //places imageview right off screen to the bottom left
rocket.backgroundColor=[UIColor grayColor];
[UIView animateWithDuration:5 animations:^(){rocket.frame=CGRectMake(345, 528, 25, 40);} completion:^(BOOL finished){if (finished)[self spawnRocket];}]; //this should hopefully make it so the object loops when it gets at the end of the screen
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
After doing all this i click run and all i see is a white screen on my iphone 6.0 simulator
ps. im running xcode 4.5.1
A few things:
UIImageView *rocket=[[UIImageView alloc]initWithFrame:...You’re not assigning an image to the image view, the best way to do this is to use:
(The root cause of your problem) You are not adding your
UIImageViewto your main view, hence it’s not being displayed. InspawnRocket, you should do:Note: Because you want this to be done in a loop, you’re gonna have to make sure your memory management is in order.
I don’t know whether you still want the rocket on screen after it’s finished moving, but if not, remember to keep a reference to the
UIImageViewandremoveFromSuperviewwhen you’re done (to prevent memory leaks).Calling
spawnRocketinviewDidLoadis probably not the best idea, it may not reach the screen yet whenspawnRocketis called. Try calling it inviewWillAppearorviewDidAppear(whatever is best in your case)[self performSelector:@selector(spawnRocket) withObject:self afterDelay:2];You don’t need to provide
selfwithinwithObject:, you’re not accepting any parameters withinspawnRocket