標籤:
又給自己挖了一個坑,我很喜歡動畫不錯,但是寫出來又是另外一個問題了~~~
這一篇我們來說說UIKit中的動畫API,其中包括:
-
UIView.UIView.animateWithDuration
-
UIView.transitionWithView
-
UIView.animateKeyframesWithDuration
今天的故事就將圍繞這些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: UIViewAnimationOptions, animations: () -> Void, completion: ((Bool) -> Void)?)
一共七個參數:
關於這些參數,選一個列子,嘗試不同的參數,這樣可以更好的理解每個參數的意義。
好醜的動畫
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進入或者離開視圖。
先看一下這一個動畫效果吧:
其中banner右移消失的動畫用的就是上面提到的UIView.UIView.animateWithDuration,而進入的動畫用的就是現在要講的UIView.transitionWithView。很像一頁書頁翻下來的感覺哈。
我們來看一下函數原型
class func transitionWithView(view: UIView, duration: NSTimeInterval, options: UIViewAnimationOptions, animations: () -> Void, completion: ((Bool) -> Void)?)
一共五個參數:
代碼大概長這樣
UIView.transitionWithView(status, duration: 0.33, options:
.CurveEaseOut | .TransitionCurlDown, animations: {
self.yourView.hidden = false
}, completion:nil
})
這一部分代碼已上傳Github,Github地址在文章的最後面。
我相信看看原始碼,大家都能明白的。
這裡再給大家看一個動畫,也是用前面提到過的函數寫的:
尼瑪~這顯示效果太差了吧,不知道你們那裡看到的是什麼樣的
仿3D效果,代碼也在上傳的那個Demo中,大家自己看啦~
到最後一個函數啦啦,UIView.animateKeyframesWithDuration,先來看一下動畫效果吧。
小飛機~飛啊飛~
我 們很容易就可以發現,這個動畫分了很多階段完成,我們當然可以用我們提到的第一個函數 UIView.UIView.animateWithDuration來完成,但是,你不覺得嵌套加嵌套顯得很不好看嗎,我們當然還有更好的方法來實現, 就是我們現在要說的,先來看一下函數原型:
class func animateKeyframesWithDuration(duration: NSTimeInterval, delay: NSTimeInterval, options: UIViewKeyframeAnimationOptions, animations: () -> Void, completion: ((Bool) -> Void)?)
一共五個參數:
來看一下我們實現這個小飛機~飛啊飛~~的代碼:
let originalCenter = planeImage.center
UIView.animateKeyframesWithDuration(1.5, delay: 0.0, options: .Repeat, animations: {
//add keyframes
UIView.addKeyframeWithRelativeStartTime(0.0, relativeDuration: 0.25, animations: {
self.planeImage.center.x += 80.0
self.planeImage.center.y -= 10.0
})
UIView.addKeyframeWithRelativeStartTime(0.1, relativeDuration: 0.4) {
self.planeImage.transform = CGAffineTransformMakeRotation(CGFloat(-M_PI_4/2))
}
UIView.addKeyframeWithRelativeStartTime(0.25, relativeDuration: 0.25) {
self.planeImage.center.x += 100.0
self.planeImage.center.y -= 50.0
self.planeImage.alpha = 0.0
}
UIView.addKeyframeWithRelativeStartTime(0.51, relativeDuration: 0.01) {
self.planeImage.transform = CGAffineTransformIdentity
self.planeImage.center = CGPoint(x: 0.0, y: originalCenter.y)
}
UIView.addKeyframeWithRelativeStartTime(0.55, relativeDuration: 0.45) {
self.planeImage.alpha = 1.0
self.planeImage.center = originalCenter
}
}, completion:nil)
完整的代碼在最後提供的原始碼中有。
事實告訴我們動畫是要靠設計的,你看我上面的動畫抽的一筆,但事實上用同樣的代碼可以寫出很漂亮的動畫。
代碼已上傳Github:https://github.com/superxlx/iOS_Animation_Test1
02-iOS核心動畫-第一課——ViewAnimations