我是编程新手,正在尝试借助“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/