Swift动画WithDuration语法是什么? [英] Whats the Swift animate WithDuration syntax?
问题描述
我正在将一个较旧的应用程序移植到Xcode 7 beta,但动画出现错误:
I'm porting an older app over to Xcode 7 beta and I'm getting an error on my animations:
无法使用类型为参数的列表调用'animateWithDuration' '((Double,delay:Double,options:nil,animations(()-> _, 完成:无)'
Cannot invoke 'animateWithDuration' with an argument list of type '(Double, delay: Double, options: nil, animations: () -> _, completion: nil)'
代码如下:
UIView.animateWithDuration(0.5, delay: 0.3, options: nil, animations: {
self.username.center.x += self.view.bounds.width
}, completion: nil)
这在Xcode 6中有效,所以我假设这是Swift中的更新.所以我的问题是:
This works in Xcode 6 so I'm assuming this is an update in Swift. So my question is:
animateWithDuration的Swift 3语法是什么?
推荐答案
Swift 3/4语法
以下是Swift 3语法的更新:
Swift 3/4 Syntax
Here's an update with the Swift 3 Syntax:
UIView.animate(withDuration: 0.5, delay: 0.3, options: [.repeat, .curveEaseOut, .autoreverse], animations: {
self.username.center.x += self.view.bounds.width
}, completion: nil)
如果您需要添加完成处理程序,只需添加一个闭包,如下所示:
If you need to add a completion handler just add a closure like so:
UIView.animate(withDuration: 0.5, delay: 0.3, options: [.repeat, .curveEaseOut, .autoreverse], animations: {
// animation stuff
}, completion: { _ in
// do stuff once animation is complete
})
旧答案:
事实证明,这是一个非常简单的修复程序,只需将options: nil
更改为options: []
.
It turns out it's a very simple fix, just change options: nil
to options: []
.
Swift 2.2语法:
UIView.animateWithDuration(0.5, delay: 0.3, options: [], animations: {
self.username.center.x += self.view.bounds.width
}, completion: nil)
发生了什么变化?
Swift 2摆脱了C样式逗号分隔的选项列表,而支持选项集(请参阅:
What changed?
Swift 2 got rid of the C-Style comma-delimited list of options in favor of option sets (see: OptionSetType). In my original question, I passed in nil
for my options, which was valid prior to Swift 2. With the updated syntax, we now see an empty option list as an empty set: []
.
带有某些选项的animateWithDuration示例如下:
An example of animateWithDuration with some options would be this:
UIView.animateWithDuration(0.5, delay: 0.3, options: [.Repeat, .CurveEaseOut, .Autoreverse], animations: {
self.username.center.x += self.view.bounds.width
}, completion: nil)
这篇关于Swift动画WithDuration语法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!