在 Swift 中增加 UIView.animateWithDuration 中的参数

Incrementing a parameter in UIView.animateWithDuration in Swift

我有一个 IBOutlet 按钮集合,我试图按顺序在屏幕上显示这些按钮。它们都从屏幕开始时很好,但是当它们进入动画时,我希望每个按钮在前一个按钮之后 0.05 秒出现在屏幕上。我不知道如何增加 UIView.animateWithDuration 中的延迟。使用下面的代码,它们会同时在屏幕上显示动画。

//outlet collection
@IBOutlet var options: [UIButton]!

let increment = 0.25

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)

    for button in options {
        button.center.y += view.bounds.height
    }
}

override func viewDidLoad() {
    super.viewDidLoad()

    for button in options {
        UIView.animateWithDuration(1.0, delay: increment, usingSpringWithDamping: 0.7, initialSpringVelocity: 0.0, options: nil, animations: {
            button.center.y -= self.view.bounds.height
            self.increment + 0.05
            }, completion: nil)
    }
}
for button in options {
        UIView.animateWithDuration(1.0, delay: increment, usingSpringWithDamping: 0.7, initialSpringVelocity: 0.0, options: nil, animations: {
            button.center.y -= self.view.bounds.height
            }, completion: nil)

    }
           increment = increment + 0.05

}

另外: 改变这个

let increment = 0.25

   var increment = 0.25

增加increment外部动画。因为animateWithDuration是一个异步方法,它会先return。所以,你所有的按钮都有 相同的延迟。

@IBOutlet weak var button1: UIButton!
@IBOutlet weak var button2: UIButton!

@IBOutlet var options: [UIButton]!

let increment = 0.25


override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)

    for button in options {
        button.center.y += view.bounds.height

    }
}

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)

        for button in options {
            UIView.animateWithDuration(1.0, delay: increment, usingSpringWithDamping: 0.7, initialSpringVelocity: 0.0, options: nil, animations: {
                button.center.y -= self.view.bounds.height
                }, completion: nil)

            self.increment + 0.05
    }
}

使用 += 时也会出现错误 "Cannot invoke '+=' with an argument list of type '(Double, FloatLiteralConvertible)'",但只需要 +

这里是实现动画之间所需延迟的方法:

var i! as UInt64;
i = 0
for button in options {
                // your animation with delay
        UIView.animateWithDuration(1.0, delay: (i *0.05), usingSpringWithDamping: 0.7, initialSpringVelocity: 0.0, options: nil, animations: {
            button.center.y -= self.view.bounds.height
            }, completion: nil)
    })
    ++i

}