[英]UIView.animateWithDuration in Swift 2.0
我是编程新手,并且正在尝试借助“Swift for Beginners:Develop and Design”这本书来学习Swift和Xcode。 到目前为止,本书一直很有用,而且我已经学到了很多东西,但是,自从这本书出版以来,Swift和Xcode似乎已经更新了,这导致了一些变化。
我目前正在尝试编写第9章中的示例内存游戏,我遇到了一个问题。 到目前为止,由Swift I的更新版本引起的任何差异都能够自己解决,但是这一点让我很难过。
导致错误的代码是这样的:
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)
})
错误消息是这样的:
无法使用类型'的参数列表调用'animateWithDuration'(Double,delay:Double,options:UIViewAnimationOptions,UIViewAnimationOptions,UIViewAnimationOptions,animations :() - >(),completion:(_) - > _)'
建议如下:
期望一个类型'的参数列表'(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)
})
看起来你的highlightTime被定义为其他类型而不是Double,在定义变量时,只需将其类型定义为,
let highlightTime: Double = 1.0
而且,这应该解决它。
感谢大家的反馈。
过了一段时间,我才弄清楚了。
我之前已将highlightTime变量声明为“highlightTime:Double”,未指定任何值。 我将其更改为“highlightTime:NSTimeInterval”,它现在正在运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.