每次出现视图时,for循环复合中的动画

时间:2012-10-19 06:05:46

标签: objective-c ios xcode

每次加载视图时,我都会尝试获取一组新的随机动画,但相反,我会让动画成倍增加并导致不可避免的应用程序崩溃!每次出现视图时如何获得重新开始的任何帮助

- (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"];

    }

}

1 个答案:

答案 0 :(得分: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
}];

希望这会有所帮助。干杯!