簡體   English   中英

Swift 2.0中的UIView.animateWithDuration

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM