2012-05-07 6 views
2

私は一連のアニメーションをループ内で繰り返し、各ループでいくつかのパラメータをランダムに変更しようとしています。ここにコードがあります。それがうまくいかない理由を知っている人は誰ですか? ボタンアクションで一度呼び出すと動作しますが、ループはありません。 ありがとう!ジュゼッペループアニメーション - 行くつもりはありません

-(IBAction)startLoop:(id)sender { 
    for (int i=1;i<10; i++) { 
     [self animation2]; 
    } 
} 


-(id) animation2 { 

    int max=500; 
    UIImage *myImage = [UIImage imageNamed:@"coccinella2.png"]; 
    CALayer *myLayer = [CALayer layer]; 
    myLayer.contents = (id)myImage.CGImage; 
    myLayer.bounds = CGRectMake(0, 0, 50, 60); 
    [myLayer setPosition:CGPointMake(arc4random()%(max), arc4random()%(max))]; 
    [myLayer setBounds:CGRectMake(0.0, 0.0, 50.0, 60.0)]; 
    [self.view.layer addSublayer:myLayer]; 


    //translation1 
    CGPoint startPt = CGPointMake(arc4random()%(max),arc4random()%(max)); 
    CGPoint endPt = CGPointMake(arc4random()%(max),arc4random()%(max)); 
    CABasicAnimation *transl1 = [CABasicAnimation animationWithKeyPath:@"position"]; 
    transl1.removedOnCompletion = FALSE; 
    transl1.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]; 
    transl1.fromValue = [NSValue valueWithCGPoint:startPt]; 
    transl1.toValue = [NSValue valueWithCGPoint:endPt]; 
    transl1.duration = 2.0; 
    transl1.fillMode = kCAFillModeForwards; 
    transl1.beginTime = 0; 

    //scale 1 
    CABasicAnimation *scale1 = [CABasicAnimation 
           animationWithKeyPath:@"transform.scale"]; 
    scale1.removedOnCompletion = FALSE; 
    [scale1 setToValue:[NSNumber numberWithInt:3]]; 
    [scale1 setDuration:2.0f]; 
    scale1.fillMode = kCAFillModeForwards; 
    scale1.beginTime = 0; 

    //rotation1 
    CABasicAnimation *rotation1 = [CABasicAnimation 
            animationWithKeyPath:@"transform.rotation.z"]; 
    rotation1.removedOnCompletion = FALSE; 
    [rotation1 setFromValue:DegreesToNumber(0)]; 
    [rotation1 setToValue:DegreesToNumber(90)]; 
    //rotation1.repeatCount = HUGE_VALF; 
    [rotation1 setDuration:2.0f]; 
    rotation1.fillMode = kCAFillModeForwards; 
    rotation1.beginTime = 0; 

    //group 
    CAAnimationGroup* group = [CAAnimationGroup animation]; 
    [group setDuration: 6.0]; 
    group.removedOnCompletion = FALSE; 
    group.fillMode = kCAFillModeForwards; 
    [group setAnimations: [NSArray arrayWithObjects:scale1, transl1, rotation1, nil]]; 
    [myLayer addAnimation: group forKey: nil]; 
} 

答えて

2

あなたのコードは注釈を10回繰り返しませんが、すぐに10のアニメーションを開始します。前のアニメーションの終了後にアニメーションを開始することを目標にしている場合は、NSTimerを試してみてください。

+0

ありがとうございます、私の目標は、アニメーションを連続的に繰り返すことです。例えば、0.1秒ごとに、どのようにメソッドを呼び出すことができますか? – Beppino66

1

あなたは、異なる時間にアニメーションの各グループをトリガするNSTimerを使用するか、このようなコードで、各グループに開始時間を設定することができます。開始時間はよりを与える使用して、アニメーションのために

group.beginTime = CACurrentMediaTime() + delay; 

正確なタイミングは、CAが別のスレッドで実行され、アプリがビジー状態になるとNSTimersのように停止しないためです。

+0

ありがとう、私の目標は、例えば0.1秒ごとにアニメーションを連続的に繰り返すことです。私はどのようにメソッド "animation2"を呼び出すことができますか? – Beppino66

+0

アニメーションを繰り返したい場合は、forループを使用せずに複数のアニメーションを作成してください。グループアニメーションのrepeatCountプロパティを設定すると、簡単な手順でシステムがそれを行います。あなたは、それぞれのステップが他のものが中断したところでピックアップしたいですか?その場合は、removedOnCompletionプロパティをfalseに設定し、fillModeをkCAFillModeForwardsに設定します。 (すべての個々のアニメーションの最後の2つのものを行い、個々のアニメーションとグループの両方についてremovedOnCompletionを設定します)。 –

関連する問題