Rutger Huijsmans
Rutger Huijsmans

Reputation: 2408

Image not being displayed after animation

I'm trying to fire off an animation and when the animation finished I want an image to stay in the UIImageView that the animation is being run in.

The image however is not being displayed in the view unless I run the animation twice. Here's my code:

@IBAction func selfRightButton(sender: UIButton){
    Visuals.endgameAnimation(winLoseAnimationView, animationFlow: "win");
    winLoseAnimationView.image = UIImage(named: "win_00012.png");
}

Here's the code for the endgameAnimation method:

static func endgameAnimation(animationImage: UIImageView, animationFlow: String){
    var counter : Int?;
    var duration : Double?;
    var animationFlowImagePrefix : String?;
    var imageList = [UIImage]();

    if (animationFlow == "bang"){
        counter = 11;
        animationFlowImagePrefix = "bang_00001_000";
        duration = 0.8;
    }
    if (animationFlow == "confetti"){
        counter = 59;
        animationFlowImagePrefix = "confetti_000";
        duration = 0.8;
    }
    if (animationFlow == "lose"){
        counter = 12;
        animationFlowImagePrefix = "lose_000";
        duration = 0.8;
    }
    if (animationFlow == "win"){
        counter = 12;
        animationFlowImagePrefix = "win_000";
        duration = 0.8;
    }

    var i : Int?;

    for (i = 0; i < counter; ++i!) {
        if (i < 9) {
            animationFlowImagePrefix! += "0";
            animationFlowImagePrefix! += String(i!+1);
        } else {
            animationFlowImagePrefix! += String(i!+1);
        }

        var imageToAppend:UIImage = UIImage(named: animationFlowImagePrefix!)!;

        imageList.append(imageToAppend);
        animationFlowImagePrefix = animationFlowImagePrefix!.substringToIndex(animationFlowImagePrefix!.endIndex.predecessor())
        animationFlowImagePrefix = animationFlowImagePrefix!.substringToIndex(animationFlowImagePrefix!.endIndex.predecessor())
    }

    animationImage.animationImages = imageList;
    animationImage.animationDuration = duration!;
    animationImage.animationRepeatCount = 1;
    animationImage.startAnimating();
}

Upvotes: 0

Views: 42

Answers (2)

Rutger Huijsmans
Rutger Huijsmans

Reputation: 2408

Fixed it by changing the order:

@IBAction func selfRightButton(sender: UIButton){
    winLoseAnimationView.image = UIImage(named: "win_00012.png");
    Visuals.endgameAnimation(winLoseAnimationView, animationFlow "win");
}

Upvotes: 0

matt
matt

Reputation: 535306

You seem to think that your code runs in this order:

Visuals.endgameAnimation(winLoseAnimationView, animationFlow: "win");
// animation ends
winLoseAnimationView.image = UIImage(named: "win_00012.png");

But that's not true. Animation takes time - that's what animation is, a change that takes place over time. But your code does not magically pause during the animation: it just goes right on. Thus the second line, setting the image, happens before the animation even has a chance to get started.

If you want something to happen after an animation, do it in the animation's completion handler. That's what it's for (and hence the name).

(In your case, there's no completion handler, because you're using image view animation, but it might be sufficient to move the second line before the first one. I'm not certain - you'll have to try it and see.)

Upvotes: 1

Related Questions