动画图标更改

时间:2018-01-29 19:32:10

标签: ios swift animation

我正在开发我的第一个应用程序,它是一个数学谜语应用程序。玩家可以获得一个暗示其中一个变量的提示 - 它基本上将一个图像替换为另一个图像。有时我会替换多个图像,所以我使用的是一个替换所有图像的循环。我希望旧图像消失并用新图像替换。另外我希望它们一个接一个地淡出,这意味着一个图像替换动画到下一个动画之间会有一个小的延迟。

func changeHintIcons () {

    var labelsArr = [[firstEquationFirstElemnt,firstEquationSecondElemnt,firstEquationThirdElemnt],[secondEquationFirstElemnt,secondEquationSecondElemnt,secondEquationThirdElemnt],[thirdEquationFirstElemnt,thirdEquationSecondElemnt,thirdEquationthirdElemnt],[fourthEquationFirstElemnt,fourthEquationSecondElemnt,fourthEquationThirdElemnt],        [fifthEquationFirstElemnt,fifthEquationSecondElemnt,fifthEquationThirdElemnt]]

    let col:Int = Int(arc4random_uniform(UInt32(gameDifficulty.stages[gameLevel].umberOfVariables)))

    let row:Int = Int(arc4random_uniform(UInt32(2))) * 2

    let var_to_show = current_equations[col][row]
    let image_name = "answer.num.\(var_to_show)"
    for i in 0..<current_equations.count {
        for j in 0..<current_equations[i].count {
            if (current_equations[i][j] == var_to_show) {
                var image_index = j
                if (j > 0) { 
                    image_index = Int(j/2)   //Converting index
                }
                labelsArr[i][image_index]!.image = UIImage(named: image_name)!  //Replacing the image
            }
        }
    }
}

最后一件事,如果我想在动画中使用而不是让图像淡出,该怎么办?我有哪些选择以及如何实施它们?

1 个答案:

答案 0 :(得分:0)

好的,我找到了答案。基本上,swift允许您通过一个接一个地投影一组图像来创建动画。跟着这些步骤: 1.将动画图像复制到资源文件夹 2.创建一个UIImages数组 3.做与我在动画功能中所做的相同的事情

主要代码 -

var animationArray = createImageArray(total: 14, imagePrefix: "hint.animation")
animationArray.append(UIImage(named: imageHintAnswer)!)
animate(imageView: labelsArr[i][image_index]!, images: animationArray)

功能 -

func createImageArray(total: Int, imagePrefix: String) -> [UIImage] {
    var imageArray:[UIImage] = []
    for imageCount in 1..<total {
        let imageName = "\(imagePrefix).\(imageCount)"
        let image = UIImage(named: imageName)!

        imageArray.append(image)
    }

    return imageArray
}

func animate(imageView: UIImageView, images: [UIImage]) {
    imageView.animationImages = images
    imageView.animationDuration = 0.7
    imageView.animationRepeatCount = 1
    imageView.startAnimating()
}