我正在将一个较旧的应用程序移植到Xcode 7 beta版,我的动画出现了一个错误:

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中的更新.所以我的问题是:

What's the Swift 3 syntax for animateWithDuration?

推荐答案

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)

Swift相关问答推荐

体积单位从夸脱转换为杯似乎关闭了

RealityKit如何获得两个相对大小相同的ModelEntity?

传递给SWIFT ResultBuilder的闭包中结果类型的对象

NSWindow中以编程方式创建的TextField快捷方式与SwiftUI

如何获取嵌套数组中项的路径以修改数组?

使用异步收集发布者值

在领域 SwiftUI 异常中使用 Apple 登录:无法识别的 Select 器发送到实例

从一个范围减go 多个范围

Swift 包管理器 Package.resolved 文件末尾的版本是什么意思?

如何更新 UserDefault 中的特定值

这是 Int64 Swift Decoding 多余的吗?

供应和普通数组中具有空值的 map 函数的行为不一致?

swift中的SequenceType和CollectionType有什么区别?

无法使用锚激活约束

Swift 覆盖实例变量

如何更改弹出框的大小

如何发送静默推送通知有效负载

为什么枚举具有计算(computed)属性但在 Swift 中没有存储属性?

枚举大小写的原始值必须是文字

如何在 iOS 上的 Swift 中获取 sharedApplication?