3

我有一个 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)
    }
}
4

3 回答 3

1
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是异步方法,所以会先返回。所以,你所有的按钮都有相同的延迟。

于 2015-05-06T02:04:26.183 回答
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)' 的参数列表调用 '+='”,但它只需要 +

于 2015-05-06T03:58:05.300 回答
-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

}
于 2015-05-06T06:14:36.887 回答