ios - Swift 2.0 中的 UIView.animateWithDuration

标签 ios swift uiviewanimation

我是编程新手,正在尝试借助“Swift for Beginners: Develop and Design”这本书来学习 Swift 和 Xcode。到目前为止,这本书对我很有帮助,我已经学到了很多东西,但是,自本书出版以来,Swift 和 Xcode 似乎已经更新,这导致了一些变化。

我目前正在尝试编写第 9 章中的示例内存游戏,但我遇到了一个问题。到目前为止,我已经能够自己找出由更新版本的 Swift 引起的任何差异,但这个问题让我很困惑。

导致错误的代码是这样的:

UIView.animateWithDuration(highlightTime,
    delay: 0.0,
    options: [.CurveLinear, .AllowUserInteraction, .BeginFromCurrentState],
    animations: {
        button.backgroundColor = highlightColor
    }, completion: { finished in
        button.backgroundColor = originalColor
        var newIndex : Int = index + 1
        self.playSequence(newIndex, highlightTime:  highlightTime)
})

错误信息是这样的:

Cannot invoke 'animateWithDuration' with an argument list of type '(Double, delay: Double, options: UIViewAnimationOptions, UIViewAnimationOptions, UIViewAnimationOptions, animations: () -> (), completion: (_) -> _)'

建议是这样的:

Expected an argument list of type '(NSTimeInterval, delay: NSTimeInterval, options: UIViewAnimationOptions, animations: () -> Void, completion: ((Bool) -> Void)?)'

如有任何帮助或见解,我们将不胜感激。

最佳答案

Swift 对转换很挑剔,所以将数字包装在 NSTimeInterval 中

UIView.animateWithDuration(NSTimeInterval(highlightTime),
    delay: NSTimeInterval(0.0),
    options: [.CurveLinear, .AllowUserInteraction, .BeginFromCurrentState],
    animations: {
        button.backgroundColor = highlightColor
    }, completion: { finished in
        button.backgroundColor = originalColor
        var newIndex : Int = index + 1
        self.playSequence(newIndex, highlightTime:  highlightTime)
})

关于ios - Swift 2.0 中的 UIView.animateWithDuration,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33092958/

相关文章:

ios - 为什么我的 GPA Double 值在 Swift 中不能正常工作?

ios - Swift Ios 设置委托(delegate)而不将 self 作为其他 View Controller

swift - 通过搜索栏创建到另一个 View Controller 的 segue?

ios - UIView.animateWithDuration 不会在单独的 View Controller 上产生动画

ios - 如何更新自动续订订阅收据

iOS popViewController 不释放内存并且不调用 dealloc

ios - 无法在控制台(调试区)使用 po 命令

ios - Swift 3 - 理解 MKAnnotationView 和 mapView() 函数

ios - 如何将 UIView 旋转 45 度?

ios - animateWithDuration 中的自定义缓动?