IBAnimatable轉(zhuǎn)場動畫-Explode學(xué)習(xí)

閱讀源碼后可以發(fā)現(xiàn)妆丘,和一般自定義轉(zhuǎn)場動畫一致鲜漩,新建繼承 NSObject 子類徘禁,遵守 UIViewControllerAnimatedTransitioning 協(xié)議疾渴。

實(shí)現(xiàn)兩個(gè)代理方法:

  • 返回動畫持續(xù)時(shí)間代理:
func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
        return 0.75
}
  • 自定義動畫代理:
func animateTransition(transitionContext: UIViewControllerContextTransitioning) {    
      // 自定義動畫函數(shù)
}

參數(shù) transitionContext 可以可以通過 func viewForKey(key: String) -> UIView? / public func viewControllerForKey(key: String) -> UIViewController? 取出轉(zhuǎn)場動畫的對應(yīng) fromView/toView / formViewController/toViewController 對應(yīng)的 key 值:

viewForKey:
UITransitionContextFromViewKey
UITransitionContextToViewKey

viewControllerForKey:
UITransitionContextFromViewControllerKey
UITransitionContextToViewControllerKey

Explode 動畫中主要在于屏幕快照的獲取以及快照的區(qū)域分剪,核心代碼:

// 獲取 fromView 的快照
let fromViewSnapshot = fromView.snapshotViewAfterScreenUpdates(false)

        // 將快照剪切成小塊加到 containerView 上
        for x in 0.0.stride(to: Double(size.width), by: Double(size.width / xFactor)) {
            for y in 0.0.stride(to: Double(size.height), by: Double(size.height / yFactor)) {
                let snapshotRegion = CGRect(x: CGFloat(x), y: CGFloat(y), width: size.width / xFactor, height: size.height / yFactor)

                // 按所給區(qū)域獲得快照的小塊
                let snapshot = fromViewSnapshot.resizableSnapshotViewFromRect(snapshotRegion, afterScreenUpdates: false, withCapInsets: UIEdgeInsetsZero)
                // 主要是設(shè)置位置
                snapshot.frame = snapshotRegion
                // 將拼成的 fromView 快照加到 containerView的最頂層
                containerView.addSubview(snapshot)
                snapshots.append(snapshot)
            }
        }

// 將 fromView 隱藏
containerView.sendSubviewToBack(fromView)

剩下的就是對 每一個(gè)小塊的動畫處理屯仗,并在動畫結(jié)束后調(diào)用:

ransitionContext.completeTransition(!transitionContext.transitionWasCancelled())

這都很簡單搞坝,難的是如何結(jié)合手勢使用,這是最值得學(xué)習(xí)的地方魁袜,理解不深桩撮,可以 clone 源碼 學(xué)習(xí)敦第。

實(shí)現(xiàn)過程主要是對 UIPercentDrivenInteractiveTransition 的學(xué)習(xí)使用,和 IBAnimatable 的實(shí)現(xiàn)不同店量,我們采用 NavigationController 管理界面芜果,在 FirstViewControllerfunc viewWillAppear(animated: Bool) {} 內(nèi)設(shè)置代理: navigationController?.delegate = self
(如果在方法: func viewDidLoad() {} 設(shè)置代理會導(dǎo)致轉(zhuǎn)場取消后無法再次進(jìn)行自定義動畫轉(zhuǎn)場)
實(shí)現(xiàn)代理方法:

extension FirstViewController: UINavigationControllerDelegate {
    func navigationController(navigationController: UINavigationController, animationControllerForOperation operation: UINavigationControllerOperation, fromViewController fromVC: UIViewController, toViewController toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        if operation == UINavigationControllerOperation.Push {
            // ExplodeAnimator 即為自定義的轉(zhuǎn)場動畫
            return ExplodeAnimator()
        } else {
            return nil
        }
    }
}

之后就是對 SecondViewController 內(nèi)進(jìn)行自定義手勢 popViewController :
首先對 view 添加返回手勢:

view.addGestureRecognizer({
            let pan = UIScreenEdgePanGestureRecognizer(target: self, action: #selector(SecondViewController.pan(_:)))
            pan.edges = UIRectEdge.Left
            return pan
        }())

手勢回調(diào)方法:

func pan(edgePan: UIScreenEdgePanGestureRecognizer) {

        let progress = edgePan.translationInView(self.view).x / self.view.bounds.width

        if edgePan.state == UIGestureRecognizerState.Began {
            self.percentDrivenTransition = UIPercentDrivenInteractiveTransition()
            self.navigationController?.popViewControllerAnimated(true)
        } else if edgePan.state == UIGestureRecognizerState.Changed {
            self.percentDrivenTransition?.updateInteractiveTransition(progress)
        } else if edgePan.state == UIGestureRecognizerState.Cancelled || edgePan.state == UIGestureRecognizerState.Ended {
            if progress > 0.5 {
                self.percentDrivenTransition?.finishInteractiveTransition()
            } else {
                self.percentDrivenTransition?.cancelInteractiveTransition()
            }
            self.percentDrivenTransition = nil
        }
    }

同樣,在 SecondViewControllerfunc viewWillAppear(animated: Bool) {} 方法內(nèi)設(shè)置代理: navigationController!.delegate = self融师,區(qū)別只是在于多實(shí)現(xiàn)一個(gè)代理方法:

extension SecondViewController: UINavigationControllerDelegate {

    func navigationController(navigationController: UINavigationController, animationControllerForOperation operation: UINavigationControllerOperation, fromViewController fromVC: UIViewController, toViewController toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        if operation == UINavigationControllerOperation.Pop {
            return ExplodeAnimator()
        } else {
            return nil
        }
    }

    func navigationController(navigationController: UINavigationController, interactionControllerForAnimationController animationController: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning? {
        if animationController is ExplodeAnimator {
            return self.percentDrivenTransition
        } else {
            return nil
        }
    }
}

大功告成右钾,
效果展示:

Demo
Demo

代碼地址: CodeDemo
IBAnimatable 源碼的實(shí)現(xiàn)基于高度的封裝旱爆,這也是望塵莫及的地方舀射。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市怀伦,隨后出現(xiàn)的幾起案子脆烟,更是在濱河造成了極大的恐慌,老刑警劉巖房待,帶你破解...
    沈念sama閱讀 218,386評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件邢羔,死亡現(xiàn)場離奇詭異,居然都是意外死亡桑孩,警方通過查閱死者的電腦和手機(jī)拜鹤,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,142評論 3 394
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來洼怔,“玉大人署惯,你說我怎么就攤上這事×土ィ” “怎么了极谊?”我有些...
    開封第一講書人閱讀 164,704評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長安岂。 經(jīng)常有香客問我轻猖,道長,這世上最難降的妖魔是什么域那? 我笑而不...
    開封第一講書人閱讀 58,702評論 1 294
  • 正文 為了忘掉前任咙边,我火速辦了婚禮,結(jié)果婚禮上次员,老公的妹妹穿的比我還像新娘败许。我一直安慰自己,他們只是感情好淑蔚,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,716評論 6 392
  • 文/花漫 我一把揭開白布市殷。 她就那樣靜靜地躺著,像睡著了一般刹衫。 火紅的嫁衣襯著肌膚如雪醋寝。 梳的紋絲不亂的頭發(fā)上搞挣,一...
    開封第一講書人閱讀 51,573評論 1 305
  • 那天,我揣著相機(jī)與錄音音羞,去河邊找鬼囱桨。 笑死,一個(gè)胖子當(dāng)著我的面吹牛嗅绰,可吹牛的內(nèi)容都是我干的舍肠。 我是一名探鬼主播,決...
    沈念sama閱讀 40,314評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼办陷,長吁一口氣:“原來是場噩夢啊……” “哼貌夕!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起民镜,我...
    開封第一講書人閱讀 39,230評論 0 276
  • 序言:老撾萬榮一對情侶失蹤啡专,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后制圈,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體们童,經(jīng)...
    沈念sama閱讀 45,680評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,873評論 3 336
  • 正文 我和宋清朗相戀三年鲸鹦,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了慧库。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,991評論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡馋嗜,死狀恐怖齐板,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情葛菇,我是刑警寧澤甘磨,帶...
    沈念sama閱讀 35,706評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站眯停,受9級特大地震影響济舆,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜莺债,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,329評論 3 330
  • 文/蒙蒙 一滋觉、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧齐邦,春花似錦椎侠、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,910評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春宣羊,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背汰蜘。 一陣腳步聲響...
    開封第一講書人閱讀 33,038評論 1 270
  • 我被黑心中介騙來泰國打工仇冯, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人族操。 一個(gè)月前我還...
    沈念sama閱讀 48,158評論 3 370
  • 正文 我出身青樓苛坚,卻偏偏與公主長得像,于是被迫代替她去往敵國和親色难。 傳聞我的和親對象是個(gè)殘疾皇子泼舱,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,941評論 2 355

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