Swift 3/4 语法
这是 Swift 3 语法的更新:
UIView.animate(withDuration: 0.5, delay: 0.3, options: [.repeat, .curveEaseOut, .autoreverse], animations: {
self.username.center.x += self.view.bounds.width
}, completion: nil)
如果您需要添加完成处理程序,只需添加一个闭包,如下所示:
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: []
.
斯威夫特 2.2 语法:
UIView.animateWithDuration(0.5, delay: 0.3, options: [], animations: {
self.username.center.x += self.view.bounds.width
}, completion: nil)
发生了什么变化?
Swift 2 摆脱了 C 风格的以逗号分隔的选项列表,转而支持选项集(参见:OptionSetType)。在我最初的问题中,我传递nil
了我的选项,这在 Swift 2 之前是有效的。使用更新的语法,我们现在看到一个空选项列表作为一个空集:[]
.
带有一些选项的 animateWithDuration 示例如下:
UIView.animateWithDuration(0.5, delay: 0.3, options: [.Repeat, .CurveEaseOut, .Autoreverse], animations: {
self.username.center.x += self.view.bounds.width
}, completion: nil)