简体   繁体   English

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

[英]Incrementing a parameter in UIView.animateWithDuration in Swift

I have an IBOutlet Collection of buttons that I am trying to present on screen sequentially.我有一个 IBOutlet 按钮集合,我试图按顺序在屏幕上显示这些按钮。 They all start off screen fine, but as they animate in, I'd like each button to be presented on screen 0.05 seconds after the previous button.它们都从屏幕开始很好,但是当它们进入动画时,我希望每个按钮在前一个按钮之后 0.05 秒出现在屏幕上。 I can't figure out how to increment the delay in UIView.animateWithDuration.我不知道如何增加 UIView.animateWithDuration 中的延迟。 With the code below, they are all animating on screen at the same time.使用下面的代码,它们都同时在屏幕上动画。

//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

}

Besides: Change this此外:改变这个

let increment = 0.25

To

   var increment = 0.25

Increase the increment outside animation.增加外部动画的increment Because animateWithDuration is an async method,it will return first .因为animateWithDuration是一个异步方法,它会先返回 So,all your button have same delay.所以,你所有的按钮都有相同的延迟。

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

Also getting error "Cannot invoke '+=' with an argument list of type '(Double, FloatLiteralConvertible)'" when using += but it will take just +使用 += 时还会收到错误“无法使用类型为 '(Double, FloatLiteralConvertible)' 的参数列表调用 '+='”,但只需要 +

Here is the way to achieve the required delay between animations:这是实现动画之间所需延迟的方法:

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

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM