0

I am trying to get a fresh set of random animations each time the view loads, but instead I am getting the animations multiplying and leading to the inevitable application meltdown! Any help on how to get a fresh start each time the view appears

- (void)startAnimation
   {
int n;

for (n=0; n<150; n++){

    UIBezierPath *bubblePath = [UIBezierPath bezierPath];
    [bubblePath moveToPoint:P(arc4random()%768, arc4random()%1024)];
    [bubblePath addCurveToPoint:P(arc4random()%768, arc4random()%1024)
                  controlPoint1:P(arc4random()%768, arc4random()%1024)
                  controlPoint2:P(arc4random()%768, arc4random()%1024)];


    CALayer *bubble = [CALayer layer];
    bubble.bounds = CGRectMake(0, 0, 10.0, 10.0);
    bubble.position = CGPointMake(arc4random()%768, arc4random()%1024);
    bubble.contents = (id)([UIImage imageNamed:@"Bubble.png"].CGImage);
    [self.view.layer addSublayer:bubble];



    CAKeyframeAnimation *bubbleAnim = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    bubbleAnim.path = bubblePath.CGPath;
    bubbleAnim.rotationMode = kCAAnimationRotateAuto;
    bubbleAnim.duration = 100;
    bubbleAnim.repeatCount = HUGE_VALF;
    [bubble addAnimation:bubbleAnim forKey:@"bubble"];

}

    int i;

    for (i=0; i<8; i++){

        UIBezierPath *tinyPath = [UIBezierPath bezierPath];
        [tinyPath moveToPoint:P(arc4random()%400+840, arc4random()%75+125)];
        [tinyPath addCurveToPoint:P(-400, arc4random()%75+150)
                    controlPoint1:P(arc4random()%450, arc4random()%200+250)
                    controlPoint2:P(arc4random()%300, arc4random()%200+150)];


        CALayer *tinyFish = [CALayer layer];
        tinyFish.bounds = CGRectMake(0, 0, 150.0, 150.0);
        tinyFish.position = CGPointMake(868, 800);
        tinyFish.contents = (id)([UIImage imageNamed:@"Fish3.png"].CGImage);
        [self.view.layer addSublayer:tinyFish];

        CAKeyframeAnimation *tinyAnim = [CAKeyframeAnimation animationWithKeyPath:@"position"];
        tinyAnim.path = tinyPath.CGPath;
        tinyAnim.rotationMode = kCAAnimationRotateAuto;
        tinyAnim.duration = 15;
        tinyAnim.repeatCount = HUGE_VALF;
        [tinyFish addAnimation:tinyAnim forKey:@"fish"];

    }

}

mangler
  • 23
  • 4
  • Well , to do it every time the view appears , you have to call this method in `viewDidAppear` of course. Animations before `viewDidAppear` like for example in `viewWillAppear` or `viewDidLoad` are not recommended. This is why: http://stackoverflow.com/a/1384584/624091 . Also , I'm not sure about the behavior that you want here, but the thing is that by calling this in `viewDidAppear` it will add all those layers each time they appear. So you should remove them from the `superlayer` after the animation is done. Not sure about the HUGE_VAL either. What is the wanted behavior? – George Oct 19 '12 at 06:37
  • I do call [self startAnimation]; in viewDidAppear, and the effect is random floating bubbles and fish swimming on the screen. The problem is that the layers never get removed and every time the viewAppears the animation multiplies! – mangler Oct 19 '12 at 13:57

1 Answers1

0
[UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationCurveLinear animations:^
{
     //do your animations here
}completion:^(BOOL finished)
{
    //remove all layers here. Eventually add here another animation where you fade out all layers and in that animation's completion block remove them from the view
}];

Hope this helps. Cheers!

George
  • 4,029
  • 1
  • 23
  • 31