2011-09-20 4 views
0

私は継続的にUIViewのアニメーションを果たしている再帰関数を持っている上の再帰関数を停止します(これを行うには良い方法があれば、私に知らせてください):確かviewDidUnload

-(void)playAnimationRecursive 
{ 
    [appDelegate commenceFishAnimation]; 
    [animationView addSubview:appDelegate.fishAnimationImageView]; 
    animationView.alpha=0.5; 

    [NSTimer scheduledTimerWithTimeInterval:14.0 target:self selector:@selector(playAnimationRecursive) userInfo:nil repeats:NO]; 
} 

ないが、これはneccessaryである場合が、ここではappDelegate commenceFishAnimationMethodです:

 upperFish=[[UIImageView alloc] initWithImage:upperFishImage]; 
     bigFish=[[UIImageView alloc] initWithImage:bigFishImage]; 
     shark=[[UIImageView alloc] initWithImage:sharkImage]; 
     groupFish=[[UIImageView alloc] initWithImage:groupFishImage]; 
     fish1=[[UIImageView alloc] initWithImage:fish1Image]; 
     fishAnimationImageView=[[[UIImageView alloc] init] retain]; 

     //set the initial position of each fish to be out of frame 
     upperFish.frame=CGRectMake(-50, 150, 119/2, 93/2); //moves east 
     bigFish.frame=CGRectMake(-280, 340, 251/2, 137/2); //moves east 
     shark.frame=CGRectMake(-100, 390, 164/2, 52/2); //moves east 
     groupFish.frame=CGRectMake(500, 320, 155/2, 89/2); //moves west 
     fish1.frame=CGRectMake(370, 280, 155/2, 89/2); //moves west 

     //add fishes to current view 
     [fishAnimationImageView addSubview:upperFish]; 
     [fishAnimationImageView addSubview:bigFish]; 
     [fishAnimationImageView addSubview:shark]; 
     [fishAnimationImageView addSubview:groupFish]; 
     [fishAnimationImageView addSubview:fish1]; 

     //animate the position of each fish view 
     [UIView beginAnimations:nil context:NULL]; 
     [UIView setAnimationDuration:10.0]; 
     [UIView setAnimationCurve:UIViewAnimationCurveLinear]; 
     upperFish.transform=CGAffineTransformMakeTranslation(150, -200); 
     bigFish.transform=CGAffineTransformMakeTranslation(600, 0); 
     shark.transform=CGAffineTransformMakeTranslation(550, 0); 
     groupFish.transform=CGAffineTransformMakeTranslation(-600, 0); 
     fish1.transform=CGAffineTransformMakeTranslation(-550, 0); 

     [UIView commitAnimations]; 
     [fishAnimationImageView release]; 
     [upperFish release]; 
     [bigFish release]; 
     [shark release]; 
     [groupFish release]; 
     [fish1 release]; 
} 

だから何が起こるかは、すべてのビューが、私はBGにこのアニメーション演劇を持っているので、私は、ビューAの上だ場合、それは再帰関数を呼び出し、継続的にアニメーションを果たしていることです。ビューBに移動すると、再帰関数も呼び出され、アニメーションが連続して再生されます。しかし、私はメモリ警告レベル2を取得している、と私は多分それはビューAはまだ再帰的なメソッドを呼び出すためだと思っている?それが理由なら、アンロード時に再帰的メソッドを停止するにはどうすればよいですか?それとも私の問題じゃないの?

EDIT:

が今ここに私のコードです(アニメーションはループしません)

-(void)commenceFishAnimation 
{ 
    UIImageView *newImageView=[[UIImageView alloc] init]; 
    self.fishAnimationImageView=newImageView; 

    upperFish=[[UIImageView alloc] initWithImage:upperFishImage]; 
    bigFish=[[UIImageView alloc] initWithImage:bigFishImage]; 
    shark=[[UIImageView alloc] initWithImage:sharkImage]; 
    groupFish=[[UIImageView alloc] initWithImage:groupFishImage]; 
    fish1=[[UIImageView alloc] initWithImage:fish1Image]; 

    //set the initial position of each fish to be out of frame 
    upperFish.frame=CGRectMake(-50, 150, 119/2, 93/2); //moves east 
    bigFish.frame=CGRectMake(-280, 340, 251/2, 137/2); //moves east 
    shark.frame=CGRectMake(-100, 390, 164/2, 52/2); //moves east 
    groupFish.frame=CGRectMake(500, 320, 155/2, 89/2); //moves west 
    fish1.frame=CGRectMake(370, 280, 155/2, 89/2); //moves west 

    //add fishes to current view 
    [fishAnimationImageView addSubview:upperFish]; 
    [fishAnimationImageView addSubview:bigFish]; 
    [fishAnimationImageView addSubview:shark]; 
    [fishAnimationImageView addSubview:groupFish]; 
    [fishAnimationImageView addSubview:fish1]; 

    //animate the position of each fish view 
    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:10.0]; 
    [UIView setAnimationCurve:UIViewAnimationCurveLinear]; 
    upperFish.transform=CGAffineTransformMakeTranslation(150, -200); 
    bigFish.transform=CGAffineTransformMakeTranslation(600, 0); 
    shark.transform=CGAffineTransformMakeTranslation(550, 0); 
    groupFish.transform=CGAffineTransformMakeTranslation(-600, 0); 
    fish1.transform=CGAffineTransformMakeTranslation(-550, 0); 

    [UIView setAnimationDelegate:self]; 
    [UIView setAnimationDidStopSelector:@selector(commenceFishAnimation)]; 
    [UIView commitAnimations]; 

    [fishAnimationImageView release]; 
    [upperFish release]; 
    [bigFish release]; 
    [shark release]; 
    [groupFish release]; 
    [fish1 release]; 
    //[newImageView release]; //if I release it, then my animation doesn't play for some reason 
} 

はその後viewDidLoadメソッド内の他のクラスでは、私はこれを行う:

[appDelegate commenceFishAnimation]; 
[animationView addSubview:appDelegate.fishAnimationImageView]; 

しかし、アニメーションは1回だけ再生されます。アニメーションをチェーンの最良の方法ではありません

答えて

3

、代わりにこれを使用する:

チェーン意志
[UIView setAnimationDidStopSelector:@selector(playAnimation)]; 

無限ループであなたのアニメーション。

また、playAnimationRecursiveの方法では、サブビューを追加し、NSTimerを解放することなく14秒ごとにスケジューリングすることに注意してください。それは本当に悪い考えです。

要約:再帰については忘れて、上記の方法を使用してください。

EDIT:私はあなたがアニメーションのデリゲートを割り当てる必要があることに言及するのを忘れ:

[UIView setAnimationDelegate:self]; //self or whatever implements playAnimation selector 
+1

そして '[UIView setAnimationDelay:4.0]'を使うと、メソッドにあった繰り返しの間に4秒の休止時間を得ることができます。 – morningstar

+0

私はあなたがお勧めしたものを試しましたが、何らかの理由でアニメーションがループしないのは1回だけです。私は 'commenceFishAnimation'メソッドの最後に' [UIView setAnimationDidStopSelector:@selector(commenceFishAnimation)];を持っています。 – Snowman

+0

アニメーションのデリゲートを割り当てる必要があることを忘れてしまいました。 [UIView setAnimationDelegate:self] [UIView commitAnimations]を呼び出す前にそれを呼び出すようにしてください。 – ender

1

は、私はあなたに参照カウントをよく理解していないお勧めいくつかの赤い旗を見ます目的C。

[[[UIImageView alloc] init] retain]; 

決して行ってはいけません。 allocは既に1の参照カウントを提供します。あなたはそれを保持する必要もありません。

fishAnimationImageView=[[[UIImageView alloc] init] retain]; 

fishAnimationImageViewによって参照される以前のオブジェクトはどうなったのですか?それはまだ保持されていますが、誰もそれを参照していません。リーク!あなたが前にそれをダブル保持し、それがあるので、あなたが正常にそのオブジェクトを解放するべきではありませんので、

[fishAnimationImageView release]; 

技術的に正しい、アプリデリゲートのインスタンス変数を(私は仮定します)。

ここにあなたがすべきことがあります。

UIImageView *newFishAnimationImageView = [[UIImageView alloc] init]; 
// retain count = 1 
self.fishAnimationImageView = newFishAnimationView; 
// retain count = 2 
// old value retain count = 0 
// assuming a typical property declaration as retain, and setter made by @synthesize 
[newFishAnimationView release]; 
// retain count = 1; not owned locally, but owned by your app delegate 

プロパティを参照すると、一般的に正しい参照カウント動作が得られます。それに従えば、あなたが割り当てたものを解放するというルールがあり、あなたが間違ってしまうケースはあまりありません。

参考カウントのドキュメントを参照してください。

また、XCodeで[製品]> [プロファイル]を試して、[リーク]を選択します。メモリリークがあるかどうか、どこにあるかを教えてくれます。

関連する問題