2015-01-15 164 views
-1

對這個抱歉很新!UIView動畫跳過第一個動畫

只要我運行按下按鈕「按下」UIView「背景」的背景顏色立即變爲藍色,然後動畫變成紫色,完全跳過動畫變成黃色,然後變成藍色。

我做錯了什麼?

@IBAction func Press(sender: AnyObject) { 

UIView.animateWithDuration(5, animations: { 
self.Background.backgroundColor = UIColor.yellowColor() 
self.Background.backgroundColor = UIColor.blueColor() 
self.Background.backgroundColor = UIColor.purpleColor() 
}, completion:{(Bool) in 
println("COLOR CHANGED") 
}) 
} 
+0

順便說一句,這個代碼是錯誤的:'(布爾)in'。它不會拋出錯誤,但它不會做你認爲它的作用。只要寫'_in'。 – matt

+0

並停止使用大寫字母作爲函數名稱和變量名稱!大寫字母用於類名。 – matt

回答

2

您不能動畫多個狀態更改到單個UIView.animateWithDuration調用中的同一個屬性。它只會動畫到最後一次狀態變化(就像你的情況一樣)。相反,您可以使用completionBlock將它們鏈接在一起。

UIView.animateWithDuration(5/3.0, animations: { 

    self.view.backgroundColor = UIColor.yellowColor() 

    }, completion:{ finished1 in 
     UIView.animateWithDuration(5/3.0, animations: { 

      self.view.backgroundColor = UIColor.blueColor() 

      }, completion:{finished2 in 
       UIView.animateWithDuration(5/3.0, animations: { 

        self.view.backgroundColor = UIColor.purpleColor() 

        }, completion:{finished3 in 

         println("COLOR CHANGED") 
       }) 
     }) 
}) 

或者您可以使用關鍵幀動畫,指定如下所示的中間幀。 relativeDuration應該是0到1之間的一個值,表示一個關鍵幀的相對持續時間。例如,如果整個動畫是3 seconds且relativeDuration是(1/3),則該關鍵幀將爲3/3 = 1秒生成動畫。

relativeStartTime類似地是關鍵幀相對於整個動畫的持續時間開始的相對時間。例如,如果整個動畫3 seconds和relativeStartTime是(1/3),那麼關鍵幀將開始後1 second

var duration = 5.0; 
var relativeDuration = 1.0/3; 

UIView.animateKeyframesWithDuration(duration, delay: 0, options: nil, animations: { 
    UIView.addKeyframeWithRelativeStartTime(0, relativeDuration: relativeDuration, animations: { 
     self.view.backgroundColor = UIColor.yellowColor() 
    }) 
    UIView.addKeyframeWithRelativeStartTime(relativeDuration, relativeDuration: relativeDuration, animations: { 
     self.view.backgroundColor = UIColor.blueColor() 
    }) 
    UIView.addKeyframeWithRelativeStartTime(2 * relativeDuration, relativeDuration: relativeDuration, animations: { 
     self.view.backgroundColor = UIColor.purpleColor() 

    }) 
    }, completion:nil); 
2

正確的,因爲這是所有一個變化一個財產。你需要製作這個三個連續不同的動畫。首先生成黃色;當這一切結束時,現在將一個全新的動畫製作成藍色;然後將全新的動畫製作成紫色。

鏈接它們的最簡單方法是將每個新動畫放入前一個動畫的完成處理程序中。像這樣(您需要更改一些其他的事情,因爲我拒絕寫代碼,其中函數和變量開始與首都):

@IBAction func press(sender: AnyObject) { 
    UIView.animateWithDuration(5.0/3.0, animations: { 
     self.background.backgroundColor = UIColor.yellowColor() 
     }, completion:{(Bool) in 
      UIView.animateWithDuration(5.0/3.0, animations: { 
       self.background.backgroundColor = UIColor.blueColor() 
       }, completion:{(Bool) in 
        UIView.animateWithDuration(5.0/3.0, animations: { 
         self.background.backgroundColor = UIColor.purpleColor() 
         }, completion:{(Bool) in 
          println("COLOR CHANGED") 
        }) 
      }) 
    }) 
} 

在iOS 8還有一個更優雅(但很難)的方式,這是使用關鍵幀動畫。但要開始,我會建議你先做簡單的事情吧!

+0

謝謝你的回答!我將如何讓所有這些代碼無限重複? – PugLvr28

+0

個人而言,我不會這樣做。我會使用關鍵幀動畫並將其設置爲無限重複。甚至可以在Core Animation級別上做所有事情;請參閱我的書,瞭解如何執行此操作:http://www.apeth.com/iOSBook/ch17.html#_core_animation – matt