本文需要環(huán)境及語言: Xcode8.0,swift3.0
EasyAnimation支持OC和swift3.0之前的版本
1. 開篇
在動畫實現(xiàn)過程中,一個看似簡單的效果往往需要大量的代碼,尤其是作用于layer上的動畫.為此有了EasyAnimation這個庫,這個庫可以將Layer Animations寫成View Animations的樣式.
關(guān)于View Animations可以看這篇:iOS動畫指南 - 1.View Animations
關(guān)于Layer Animations可以看這篇:iOS動畫指南 - 2.Layer Animations的基本使用以及iOS動畫指南 - 3.Layer Animations的進階使用
EasyAnimation是老外寫的動畫庫,目前有2K多star.
直達:https://github.com/icanzilb/EasyAnimation
話不多說上代碼.例如,要做一個漸變的圓角,以前我們會這樣做:
let cornerRadius = CABasicAnimation(keyPath: "cornerRadius")
cornerRadius.isRemovedOnCompletion = false
cornerRadius.fillMode = kCAFillModeBoth
cornerRadius.fromValue = 0
cornerRadius.toValue = 20
cornerRadius.duration = 0.5
cornerRadius.beginTime = CACurrentMediaTime() + 0.3
dogImageView.layer.add(cornerRadius, forKey: nil)
用了EasyAnimation可以直接這樣:
UIView.animate(withDuration: 0.5, delay: 0.33, options: [], animations: {
self.dogImageView.layer.cornerRadius = 20
}, completion: nil)
2. 基本使用
1. 簡單使用
// 重復(fù) 往返 有一個開始和結(jié)束的加速度
UIView.animate(withDuration: 2.0, delay: 0, options: [.repeat, .autoreverse, .curveEaseInOut], animations: {
// 位置的修改不用EasyAnimation也可以做到
self.dogImageView.layer.position.x += 180
// 圓角和邊框?qū)挾? self.dogImageView.layer.cornerRadius = 20
self.dogImageView.layer.borderWidth = 2
// 添加一個3D旋轉(zhuǎn)效果
var trans3d = CATransform3DIdentity
trans3d.m34 = -1.0/500.0
let rotationTransform = CATransform3DRotate(trans3d, CGFloat(-M_PI_4), 0.0, 1.0, 0.0)
let translationTransform = CATransform3DMakeTranslation(-50.0, 0, 0)
self.dogImageView.layer.transform = CATransform3DConcat(rotationTransform, translationTransform)
}, completion: nil)
怎么樣使用起來是不是要優(yōu)雅的多啊!用EasyAnimation還有一個好處就是:不會像Layer Animations那樣控件位置明明發(fā)生了移動,但卻還在原先的位置,也就是position.x
不是真實的,EasyAnimation就不會有這樣的煩惱啦!所有位置都是真實的.
2. 兼容iOS8及之前版本的彈性效果
利用核心動畫實現(xiàn)彈性效果這個特性是在iOS9時出來的,如果要使用需要適配iOS9之前的版本.有一個庫RBBAnimation可以實現(xiàn),在EasyAnimation中,iOS9之前是用的RBBAnimation,iOS9是用的CASpringAnimation,所以彈性效果的適配EasyAnimation已經(jīng)做好啦!
UIView.animateAndChain(withDuration: 2.0, delay: 0.0, usingSpringWithDamping: 0.25, initialSpringVelocity: 0.0, options: [], animations: {
self.dogImageView.layer.position.x += 180
self.dogImageView.layer.cornerRadius = 20
self.dogImageView.layer.borderWidth = 2
self.dogImageView.layer.transform = CATransform3DConcat(
CATransform3DMakeRotation(CGFloat(-M_PI_2), 0.0, 0.0, 1.0),
CATransform3DMakeScale(1.33, 1.33, 1.33)
)
}, completion: nil).animate(withDuration: 2.0, delay: 0.0, options: .repeat, animations: {
// 回到初始位置,重復(fù)執(zhí)行動畫
self.dogImageView.layer.transform = CATransform3DIdentity
self.dogImageView.layer.cornerRadius = 0.0
self.dogImageView.layer.position.x -= 180
}, completion: nil)
3. 動畫組
可能有童鞋注意到了彈性效果回到初始位置的是直接放到了.animate
這個閉包中.
EasyAnimation也提供了更加優(yōu)雅的組動畫實現(xiàn).
chain = UIView.animateAndChain(withDuration: 1.0, delay: 0.0, options: [], animations: {
self.dogImageView.center.x += 150.0
}, completion: nil).animate(withDuration: 1.0, animations: {
self.dogImageView.center.y += 150.0
}).animate(withDuration: 1.0, delay: 0.0, usingSpringWithDamping: 0.2, initialSpringVelocity: 0.0, options: [], animations: {
self.dogImageView.transform = CGAffineTransform(rotationAngle: CGFloat(-M_PI_2))
}, completion: nil).animate(withDuration: 0.5, animations: {
self.dogImageView.center.x -= 150.0
}).animate(withDuration: 2.0, delay: 0.0, usingSpringWithDamping: 0.2, initialSpringVelocity: 0.0, options: .repeat, animations: {
self.dogImageView.center.y -= 150.0
self.dogImageView.transform = CGAffineTransform.identity
}, completion: nil)
EasyAnimation充分利用了swift的鏈式編程特性.相比View Animations的組動畫和Layer Animations組動畫都簡化了不少.
當(dāng)然這個組動畫也提供了終止設(shè)定.
// 需要當(dāng)前執(zhí)行的部分停止后才能執(zhí)行停止這個動作
delay(seconds: 4.0) {
self.chain.cancelAnimationChain()
}
3. 實現(xiàn)原理
相信有很多童鞋都發(fā)現(xiàn)了EasyAnimation的有些方法怎么和系統(tǒng)的這么像.對的,不是像其實就是.它是怎么做到的呢?
fileprivate static func replaceAnimationMethods() {
//replace actionForLayer...
method_exchangeImplementations(
class_getInstanceMethod(self, #selector(UIView.action(for:forKey:))),
class_getInstanceMethod(self, #selector(UIView.EA_actionForLayer(_:forKey:))))
//replace animateWithDuration...
method_exchangeImplementations(
class_getClassMethod(self, #selector(UIView.animate(withDuration:animations:))),
class_getClassMethod(self, #selector(UIView.EA_animate(withDuration:animations:))))
method_exchangeImplementations(
class_getClassMethod(self, #selector(UIView.animate(withDuration:animations:completion:))),
class_getClassMethod(self, #selector(UIView.EA_animate(withDuration:animations:completion:))))
method_exchangeImplementations(
class_getClassMethod(self, #selector(UIView.animate(withDuration:delay:options:animations:completion:))),
class_getClassMethod(self, #selector(UIView.EA_animate(withDuration:delay:options:animations:completion:))))
method_exchangeImplementations(
class_getClassMethod(self, #selector(UIView.animate(withDuration:delay:usingSpringWithDamping:initialSpringVelocity:options:animations:completion:))),
class_getClassMethod(self, #selector(UIView.EA_animate(withDuration:delay:usingSpringWithDamping:initialSpringVelocity:options:animations:completion:))))
}
其實EasyAnimation需要導(dǎo)入的庫的方法就兩個:
// 組動畫的兩個方法
animateAndChain(withDuration:delay:options: animations:completion:)
animateAndChain(withDuration:delay:usingSpringWithDamping:initialSpringVelocity:options:animations:completion:)
原理:
EasyAnimation將上面代碼放到了UIView的extension中,在EasyAnimation的initialize()初始化方法中調(diào)用上面replaceAnimationMethods方法,而replaceAnimationMethods方法是通過使用runtime的
方法替換
,將自己自定義的方法與系統(tǒng)的方法進行替換.
所以如果在使用cocoapods的情況下,即使不導(dǎo)入EasyAnimation這個庫,那么上面的代碼也會被執(zhí)行.
Swift的Runtime和OC的Runtime是一回事嗎?
是的. swift的runtime底層其實還是交給OC的Runtime去執(zhí)行的.我們都知道OC是門動態(tài)語言,而Runtime只能在動態(tài)語言中才能使用,但swift雖兼顧了動態(tài)語言的語法特性,但本質(zhì)上還是門靜態(tài)語言.因為有動態(tài)語言的語法特性,所以Runtime也是可以使用的.
什么情況下Swift可以進行方法替換?
對于繼承于NSObject的類跷车,通過runtime可以獲取到的方法,從而可以完成替換.對于沒有繼承的swift的純類,就不可以了.繼承于NSObject的類為了能夠被運行時調(diào)用,又不被Swift靜態(tài)優(yōu)化,會自動添加
@objc
和dynamic
關(guān)鍵字完成動態(tài)替換.
具體細節(jié)可以看這篇文章:Swift Runtime分析:還像OC Runtime一樣嗎棘利?
4. 總結(jié):
本篇大致介紹了下EasyAnimation的使用及原理,有興趣的童鞋不妨去試試,還是挺好用的,作者一直在維護,已經(jīng)更新到swift3.0了.??
本文整理自:iOS.Animations.by.Tutorials.v2.0
源碼 : https://github.com/DarielChen/DemoCode
如有疑問,歡迎留言 :-D