在 swift 中创建多个子视图时添加动画

adding animation when creating multiple subviews in swift

我在使用 addsubview 创建按钮时遇到动画问题。即使我在循环中增加动画的延迟,它们也会同时出现。我将感谢任何能提供帮助的人。

func createButton () {
    var setx = 50
    var sety = 100
    var delay = 0.4
    var wordsInCharacters = [String]()
    for letter in "RAILROAD".characters{
        wordsInCharacters.append("\(letter)")
    }
    while wordsInCharacters.count > 0 {
        let randomIndex = Int(arc4random_uniform(UInt32(wordsInCharacters.count)))
        // Creating the buttons
        let button = SpringButton()
        button.frame = CGRect(x: setx, y: sety, width: 64, height: 64)
        button.setTitle( "\(wordsInCharacters[randomIndex])", for: .normal)
        button.setTitleColor(UIColor.white, for: .normal)
        button.backgroundColor = UIColor.gray
        button.titleLabel?.font = UIFont(name: "HelveticaNeue", size: 30)
        // Add animation
        UIView.animate(withDuration: 1, delay: delay, options: .curveEaseInOut, animations: {
            self.view.addSubview(button)
        }, completion: nil)
        if setx <= 200 {
            setx += 100
        }
        else{
            setx = 50
            sety += 100
        }
        wordsInCharacters.remove(at: randomIndex)
        delay += 0.2

    }
}

为了制作简单的动画,请不要在动画块中添加视图。 首先将 Button Alpha 值设置为 0,然后将按钮添加为子视图。 在动画块中,您将 button.alpha 设置为 1。瞧...