iOS動畫指南 - 7.簡化動畫實現(xiàn)的EasyAnimation庫

本文需要環(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)化,會自動添加@objcdynamic關(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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市朽缴,隨后出現(xiàn)的幾起案子善玫,更是在濱河造成了極大的恐慌,老刑警劉巖密强,帶你破解...
    沈念sama閱讀 218,284評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件茅郎,死亡現(xiàn)場離奇詭異,居然都是意外死亡或渤,警方通過查閱死者的電腦和手機系冗,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,115評論 3 395
  • 文/潘曉璐 我一進店門薪鹦,熙熙樓的掌柜王于貴愁眉苦臉地迎上來掌敬,“玉大人,你說我怎么就攤上這事池磁”己Γ” “怎么了?”我有些...
    開封第一講書人閱讀 164,614評論 0 354
  • 文/不壞的土叔 我叫張陵地熄,是天一觀的道長舀武。 經(jīng)常有香客問我,道長离斩,這世上最難降的妖魔是什么银舱? 我笑而不...
    開封第一講書人閱讀 58,671評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮跛梗,結(jié)果婚禮上寻馏,老公的妹妹穿的比我還像新娘。我一直安慰自己核偿,他們只是感情好诚欠,可當(dāng)我...
    茶點故事閱讀 67,699評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著漾岳,像睡著了一般轰绵。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上尼荆,一...
    開封第一講書人閱讀 51,562評論 1 305
  • 那天左腔,我揣著相機與錄音,去河邊找鬼捅儒。 笑死液样,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的巧还。 我是一名探鬼主播鞭莽,決...
    沈念sama閱讀 40,309評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼麸祷!你這毒婦竟也來了澎怒?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,223評論 0 276
  • 序言:老撾萬榮一對情侶失蹤阶牍,失蹤者是張志新(化名)和其女友劉穎喷面,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體荸恕,經(jīng)...
    沈念sama閱讀 45,668評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡乖酬,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,859評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了融求。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片咬像。...
    茶點故事閱讀 39,981評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖生宛,靈堂內(nèi)的尸體忽然破棺而出县昂,到底是詐尸還是另有隱情,我是刑警寧澤陷舅,帶...
    沈念sama閱讀 35,705評論 5 347
  • 正文 年R本政府宣布倒彰,位于F島的核電站,受9級特大地震影響莱睁,放射性物質(zhì)發(fā)生泄漏待讳。R本人自食惡果不足惜芒澜,卻給世界環(huán)境...
    茶點故事閱讀 41,310評論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望创淡。 院中可真熱鬧痴晦,春花似錦、人聲如沸琳彩。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,904評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽露乏。三九已至碧浊,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間瘟仿,已是汗流浹背箱锐。 一陣腳步聲響...
    開封第一講書人閱讀 33,023評論 1 270
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留猾骡,地道東北人瑞躺。 一個月前我還...
    沈念sama閱讀 48,146評論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像兴想,于是被迫代替她去往敵國和親幢哨。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,933評論 2 355

推薦閱讀更多精彩內(nèi)容

  • 其實醫(yī)生最想要的是嫂便,創(chuàng)造一個個生命的奇跡捞镰。作為醫(yī)生,我有點慚愧毙替,看完了幾部有關(guān)醫(yī)生的電視連續(xù)劇岸售,對醫(yī)生這行職業(yè)越來...
    一縷桂花閱讀 319評論 2 1
  • 選擇從溫尼科特的端口進入心理學(xué)的汪洋,我想我是幸運的厂画,因為曾經(jīng)是嬰兒的角色凸丸,現(xiàn)在是母親的角色,更能貼...
    仙兒lx閱讀 724評論 0 0
  • 寒假漫漫袱院,春節(jié)沒新意屎慢,何不跟著十位巨星一起記單詞,我?guī)湍阊埩巳缦率缓萌R塢巨星陪你忽洛。 ?????????????...
    玩英語閱讀 894評論 0 5
  • 從第一次寫下自已的考研狀態(tài)腻惠,到現(xiàn)在,已經(jīng)過了百來天欲虚。這段時間經(jīng)歷了很多集灌,歷練些許。 目標(biāo)院校從最初的上海國家會計學(xué)...
    嗯哼4是我閱讀 123評論 0 0