這一篇我們來說說UIKit中的動畫API,期中包括:
UIView.UIView.animateWithDuration
UIView.transitionWithView
UIView.animateKeyframesWithDuration
UIView.addKeyframeWithRelativeStartTime
今天的故事就將圍繞這些API展開,闡述他的前世今生。
UIKit動畫API使用起來十分簡單與方便,他避免了Core Animation的復雜性,雖然事實上UIKit動畫API的底層使用的也是Core Animation。
來看第一個,UIView.UIView.animateWithDuration
先來看一下函數原型:
class func animateWithDuration(duration: NSTimeInterval, delay: NSTimeInterval, usingSpringWithDamping dampingRatio: CGFloat, initialSpringVelocity velocity: CGFloat, options: UIViewAnimationOptio
一共七個參數:
duration
表示動畫執行時間。
delay
動畫延遲時間。
usingSpringWithDamping
表示彈性屬性。
initialSpringVelocity
初速度。
options
可選項,一些可選的動畫效果,包括重復等。
animations
表示執行的動畫內容,包括透明度的漸變,移動,縮放。
completion
表示執行完動畫后執行的內容。
關于這些參數,選一個列子,嘗試不同的參數,這樣可以更好的理解每個參數的意義。
好丑的動畫
self.animationView2.alpha = 0
self.animationView3.alpha = 0
UIView.animateWithDuration(5, delay: 0.5, usingSpringWithDamping: 0.5, initialSpringVelocity: 0, options: .Repeat, animations: { () -> Void in
self.animationView.center.y += 100
}) { (Bool) -> Void in
println("finish")
}
UIView.animateWithDuration(5, delay: 0.5, usingSpringWithDamping: 0.5, initialSpringVelocity: 0, options: .Repeat, animations: { () -> Void in
self.animationView2.alpha = 1
}) { (Bool) -> Void in
println("finish")
}
UIView.animateWithDuration(5, delay: 0.5, usingSpringWithDamping: 0.5, initialSpringVelocity: 0, options: .Repeat, animations: { () -> Void in
self.animationView3.center.y -= 100
self.animationView3.alpha = 1
}) { (Bool) -> Void in
println("finish")
}
代碼就不逐行解釋,三個UIView.animateWithDuration分別操作三個方塊。第一個方塊是一個移動動畫,第二個方塊是一個透明度漸變動畫,第三個方塊是移動加透明度漸變的組合動畫,可能看的不是很清楚。不得不說這個動畫真的很丑~~~
UIView.UIView.animateWithDuration這個API說穿了就是逐漸改變UIView的某項屬性,這些屬性包括:位置,大小,透明度,顏色等等。
再來看一下UIView.transitionWithView,這是一個過度動畫,主要用于UIView進入或者離開視圖。
新聞熱點
疑難解答