1

オブジェクトが画面下部で連続的に移動するように、単純にループを作成したいと思います。これが私のコードです。かなり理解しやすいはずです。

@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

これをすべて行った後、[実行]をクリックすると、iphone6.0シミュレーターの白い画面が表示されます。

ps。xcode4.5.1を実行しているim

4

2 に答える 2

1

いくつかのこと:

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

    画像を画像ビューに割り当てていない場合、これを行う最良の方法は次を使用することです。

    UIImage* image = [UIImage imageNamed:@"image.png"];
    UIImageView *rocket = [[UIImageView alloc] initWithImage:image];
    rocket.frame = CGRectMake(-25, 528, 25, 40);
    
  2. UIImageView(問題の根本的な原因)メインビューに自分を追加していないため、表示されていません。でspawnRocket、次のことを行う必要があります。

    [self.view addSubview:rocket];
    

    注:これをループで実行する必要があるため、メモリ管理が正常に行われていることを確認する必要があります。

    ロケットの移動が終了した後も、ロケットを画面に表示したいかどうかはわかりませんが、そうでない場合は、(メモリリークを防ぐために)終了時にUIImageViewとを参照することを忘れないでください。removeFromSuperview

  3. 電話spawnRocketをかけるのviewDidLoadはおそらく最善の方法ではありません。電話をかけたときにまだ画面に表示されない場合spawnRocketがあります。viewWillAppearまたはviewDidAppear(あなたの場合に最適なものは何でも)でそれを呼んでみてください

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

    self内で指定する必要はありません。内withObject:でパラメータを受け入れていません。spawnRocket

于 2012-11-03T19:10:34.547 に答える
0

UIImageView親ビューにを追加しません。メモリに保存されますが、表示されません。作成後、ViewControllerのビューに追加します。

[self.view addSubview:rocket];
于 2012-11-03T19:07:09.157 に答える