让对象在屏幕上移动iphone?
我只想有一个循环,这样一个对象就可以在屏幕底部连续移动。这是我的代码,应该很容易理解让对象在屏幕上移动iphone?,iphone,xcode,animation,uiimageview,Iphone,Xcode,Animation,Uiimageview,我只想有一个循环,这样一个对象就可以在屏幕底部连续移动。这是我的代码,应该很容易理解 @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; [self performSelector:@selector(spawnRocket) withObject:self afterDel
@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
完成所有这些之后,我点击run,我看到的只是iphone 6.0模拟器上的一个白色屏幕
ps.im运行xcode 4.5.1您不会将
UIImageView
添加到任何父视图中。它只会存在于内存中,但不会显示。创建视图控制器后,将其添加到视图控制器的视图中:
[self.view addSubview:rocket];
您不会将
UIImageView
添加到任何父视图。它只会存在于内存中,但不会显示。创建视图控制器后,将其添加到视图控制器的视图中:
[self.view addSubview:rocket];
有几件事:
UIImageView*rocket=[[UIImageView alloc]initWithFrame:…
您没有将图像指定给图像视图,最好的方法是使用:
UIImage* image = [UIImage imageNamed:@"image.png"];
UIImageView *rocket = [[UIImageView alloc] initWithImage:image];
rocket.frame = CGRectMake(-25, 528, 25, 40);
UIImageView
添加到主视图,因此它不会显示。在UIImageView
中,您应该执行以下操作:
[self.view addSubview:rocket];
注意:因为您希望在循环中完成此操作,所以必须确保内存管理正常
我不知道在火箭完成移动后,您是否仍希望它出现在屏幕上,但如果不希望,请记住在移动后保留对UIImageView
和removeFromSuperview
的引用(以防止内存泄漏)viewDidLoad
中调用spawnRocket
可能不是最好的主意,在调用spawnRocket
时,它可能还没有到达屏幕。尝试在viewwillbeen
或viewdidObeen
中调用它(无论您的情况如何)[自执行选择器:@selector(spawnRocket)with object:self afterDelay:2];
您不需要在withObject:
中提供self
,您不接受中的任何参数
UIImageView*rocket=[[UIImageView alloc]initWithFrame:…
您没有将图像指定给图像视图,最好的方法是使用:
UIImage* image = [UIImage imageNamed:@"image.png"];
UIImageView *rocket = [[UIImageView alloc] initWithImage:image];
rocket.frame = CGRectMake(-25, 528, 25, 40);
UIImageView
添加到主视图,因此它不会显示。在UIImageView
中,您应该执行以下操作:
[self.view addSubview:rocket];
注意:因为您希望在循环中完成此操作,所以必须确保内存管理正常
我不知道在火箭完成移动后,您是否仍希望它出现在屏幕上,但如果不希望,请记住在移动后保留对UIImageView
和removeFromSuperview
的引用(以防止内存泄漏)viewDidLoad
中调用spawnRocket
可能不是最好的主意,在调用spawnRocket
时,它可能还没有到达屏幕。尝试在viewwillbeen
或viewdidObeen
中调用它(无论您的情况如何)[自执行选择器:@selector(spawnRocket)with object:self afterDelay:2];
您不需要在withObject:
中提供self
,您不接受中的任何参数
另外,几个月前我已经有2个帐户,他们被禁止了,因为我问了一些不好的问题。我不确定你是否可以在stackoverflow上拥有多个帐户,所以如果你不被允许,我在这里被禁止,我将不再创建帐户。尽管我意识到这个网站有多么重要,我也不会做任何不必要的事情估计。可能值得提供如何显示/创建
ViewController
,这可能是一个问题。另外,几个月前我已经有两个帐户,他们被禁止,因为我问了一些不好的问题。我不确定是否允许你在stackoverflow上拥有多个帐户,因此如果你不允许,我在这里被禁止,我将我不会再做账户了。虽然我意识到这个网站有多么重要,我也不会提出任何不必要的问题。也许值得提供ViewController
是如何呈现/创建的,这可能是一个问题。是的,但我想设置背景颜色就足以看到它是否在iPhone 5之前的设备上运行?其他rwise您的y坐标太高。另外,通常使用NSLog。查看您的无效方法是否实际执行。是的,但我认为设置背景颜色足以查看您是否在iPhone 5之前的设备上运行它?否则您的y坐标太高。另外,通常使用NSLog。查看您的无效方法是否实际执行切。