需要帮助在swift中增加UIView.animateWithDuration中的参数

时间:2015-05-06 01:55:45

标签: ios swift delay increment animatewithduration

提前感谢任何帮助。我有一个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)

    }

}

再次感谢您的帮助!

3 个答案:

答案 0 :(得分:0)

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是异步方法,因此首先返回。因此,您的所有按钮都有相同的延迟。

答案 1 :(得分:0)

@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
    }
}

当使用+ =时,也会收到错误“无法使用'(Double,FloatLiteralConvertible)'类型的参数列表调用'+ =',但只需+ +

答案 2 :(得分:-1)

以下是实现动画之间所需延迟的方法:

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

}