简体   繁体   English

使对象在iPhone屏幕上移动?

[英]make an object move across the screen iphone?

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 完成所有这些操作后,我单击“运行”,然后看到的只是iphone 6.0模拟器上的白屏

ps. PS。 im running xcode 4.5.1 我正在运行xcode 4.5.1

A few things: 一些东西:

  1. UIImageView *rocket=[[UIImageView alloc]initWithFrame:...

    You're not assigning an image to the image view, the best way to do this is to use: 您没有将图像分配给图像视图,执行此操作的最佳方法是使用:

     UIImage* image = [UIImage imageNamed:@"image.png"]; UIImageView *rocket = [[UIImageView alloc] initWithImage:image]; rocket.frame = CGRectMake(-25, 528, 25, 40); 
  2. (The root cause of your problem) You are not adding your UIImageView to your main view, hence it's not being displayed. (问题的根本原因)您没有将UIImageView添加到主视图,因此不会显示它。 In spawnRocket , you should do: spawnRocket ,您应该执行以下操作:

     [self.view addSubview:rocket]; 

    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 UIImageView and removeFromSuperview when you're done (to prevent memory leaks). 我不知道火箭完成移动后是否仍要在屏幕上显示火箭,但是如果不希望,请记住在完成后保留对UIImageView的引用和removeFromSuperview (以防止内存泄漏)。

  3. Calling spawnRocket in viewDidLoad is probably not the best idea, it may not reach the screen yet when spawnRocket is called. 调用spawnRocketviewDidLoad可能不是最好的主意,它可能无法到达屏幕但当spawnRocket被调用。 Try calling it in viewWillAppear or viewDidAppear (whatever is best in your case) 尝试在viewWillAppearviewDidAppear调用它(以您的情况viewDidAppear

  4. [self performSelector:@selector(spawnRocket) withObject:self afterDelay:2];

    You don't need to provide self within withObject: , you're not accepting any parameters within spawnRocket 您不需要在withObject:提供self也不需要在spawnRocket接受任何参数

You don't add the UIImageView to any parent view. 您不会将UIImageView添加到任何父视图。 It will just live in memory, but not be displayed. 它只会存在于内存中,而不会显示。 Add it to your view controller's view after creating it: 创建后将其添加到视图控制器的视图中:

[self.view addSubview:rocket];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM