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
})
Old Answer:
事实证明,这是一个非常简单的修复方法,只需将options: nil
改为options: []
.
Swift 2.2 Syntax:
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)