动画后图像未显示

时间:2015-08-14 17:31:54

标签: ios swift animation

我试图触发动画,当动画完成时,我希望图像保留在运行动画的UIImageView中。

然而,除非我运行动画两次,否则图像不会显示在视图中。这是我的代码:

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

这里是endgameAnimation方法的代码:

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();
}

2 个答案:

答案 0 :(得分:1)

您似乎认为您的代码按此顺序运行:

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

但事实并非如此。动画需要时间 - 这就是动画,随着时间的推移发生的变化。但是你的代码在动画期间并没有神奇地暂停:它就是正确的。因此设置图像的第二行在之前发生动画甚至有机会启动

如果您希望在动画之后发生某些事情,请在动画的完成处理程序中执行此操作。这就是它的用途(因而也就是名称)。

(在你的情况下,没有完成处理程序,因为你正在使用图像视图动画,但是在第一行之前移动第二行可能就足够了。我不确定 - 你必须尝试它并看到。)

答案 1 :(得分:0)

通过更改顺序修正了它:

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