自定義彈出/消失動畫

import UIKit

class Popoveranimator: NSObject {
    //MARK:- 對外提供的屬性
    var presentedFrame : CGRect = CGRect.zero
    var isPresented : Bool = false

    var callBack : ((Bool) -> ())?
    
    //MARK:- 自定義構(gòu)造函數(shù)
    init(callBack : @escaping (Bool) -> ()) {
        self.callBack = callBack
    }
    
}

//MARK:- 自定義轉(zhuǎn)場代理的方法
extension Popoveranimator : UIViewControllerTransitioningDelegate {
    //目的:改變彈出view的尺寸
    func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {
        
        let presentation = PoppresentationController(presentedViewController: presented, presenting: presenting)
        presentation.presentedFrame = presentedFrame
        
        return presentation
    }
    
    //目的:自定義彈出的動畫
    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        
        isPresented = true
        callBack!(isPresented)
        return self
    }
    
    //目的:自定義消失的動畫
    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        
        isPresented = false
        callBack!(isPresented)
        return self
    }
}


//MARK:- 彈出和消失動畫代理的方法
extension Popoveranimator : UIViewControllerAnimatedTransitioning {
    //動畫執(zhí)行的時間
    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        return 0.5
    }
    
    //獲取轉(zhuǎn)場的上下文:可以通過轉(zhuǎn)場上下文獲取彈出的view和消失的view
    //UItransitionContextFromViewKey : 獲取消失的view
    //UITransitionContextToViewKey : 獲取彈出的View
    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        isPresented ? animationForPresentedView(using: transitionContext) : animationForDismissedView(using: transitionContext)
    }
    
    //自定義彈出動畫
    private func animationForPresentedView(using transitionContext: UIViewControllerContextTransitioning) {
        //獲取彈出的view
        let presentedView = transitionContext.view(forKey: .to)!
        
        //將彈出的view添加到containerView
        transitionContext.containerView.addSubview(presentedView)
        
        //執(zhí)行動畫
        presentedView.transform = CGAffineTransform(scaleX: 1.0, y: 0.0)
        presentedView.layer.anchorPoint = CGPoint(x: 0.5, y: 0)
        UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {
            presentedView.transform = CGAffineTransform.identity
        }) { (_) in
            //必須告訴轉(zhuǎn)場上下文你已經(jīng)完成動畫
            transitionContext.completeTransition(true)
        }
    }
    
    //自定義消失動畫
    private func animationForDismissedView(using transitionContext: UIViewControllerContextTransitioning) {
        //獲取彈出的view
        let dismissedView = transitionContext.view(forKey: .from)!
        
        //執(zhí)行動畫
        UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {
            dismissedView.transform = CGAffineTransform(scaleX: 1.0, y: 0.001)
        }) { (_) in
            //必須告訴轉(zhuǎn)場上下文你已經(jīng)完成動畫
            transitionContext.completeTransition(true)
        }
    }
}

import UIKit

class PhotoBrowserAnimator: NSObject {
    var isPresented : Bool = false
    
}


extension PhotoBrowserAnimator : UIViewControllerTransitioningDelegate {
    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresented = true
        return self
    }
    
    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresented = false
        return self
    }
}


extension PhotoBrowserAnimator : UIViewControllerAnimatedTransitioning {
    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        return 0.5
    }
    
    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        isPresented ? animationForPresentedView(using: transitionContext) : animationForDismissView(using: transitionContext)
    }
    
    //自定義彈出動畫
    func animationForPresentedView(using transitionContext: UIViewControllerContextTransitioning) {
        //取出彈出的view
        let presentedView = transitionContext.view(forKey: UITransitionContextViewKey.to)!
        
        //將presentedView添加到containerView中
        transitionContext.containerView.addSubview(presentedView)
        
        //執(zhí)行動畫
        presentedView.alpha = 0
        UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {
            presentedView.alpha = 1.0
        }) { (_) in
            transitionContext.completeTransition(true)
        }
    }
    
    //自定義消失動畫
    func animationForDismissView(using transitionContext: UIViewControllerContextTransitioning) {
        //取出消失的view
        let dismissView = transitionContext.view(forKey: UITransitionContextViewKey.from)!
        
        //執(zhí)行動畫
        UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {
            dismissView.alpha = 0
        }) { (_) in
            dismissView.removeFromSuperview()
            transitionContext.completeTransition(true)
        }
    }
    
    
}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末画畅,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子宋距,更是在濱河造成了極大的恐慌轴踱,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,183評論 6 516
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件谚赎,死亡現(xiàn)場離奇詭異淫僻,居然都是意外死亡,警方通過查閱死者的電腦和手機壶唤,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,850評論 3 399
  • 文/潘曉璐 我一進店門雳灵,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人闸盔,你說我怎么就攤上這事悯辙。” “怎么了迎吵?”我有些...
    開封第一講書人閱讀 168,766評論 0 361
  • 文/不壞的土叔 我叫張陵躲撰,是天一觀的道長。 經(jīng)常有香客問我击费,道長拢蛋,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,854評論 1 299
  • 正文 為了忘掉前任蔫巩,我火速辦了婚禮谆棱,結(jié)果婚禮上快压,老公的妹妹穿的比我還像新娘。我一直安慰自己垃瞧,他們只是感情好蔫劣,可當我...
    茶點故事閱讀 68,871評論 6 398
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著皆警,像睡著了一般拦宣。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上信姓,一...
    開封第一講書人閱讀 52,457評論 1 311
  • 那天,我揣著相機與錄音绸罗,去河邊找鬼意推。 笑死,一個胖子當著我的面吹牛珊蟀,可吹牛的內(nèi)容都是我干的菊值。 我是一名探鬼主播,決...
    沈念sama閱讀 40,999評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼育灸,長吁一口氣:“原來是場噩夢啊……” “哼腻窒!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起磅崭,我...
    開封第一講書人閱讀 39,914評論 0 277
  • 序言:老撾萬榮一對情侶失蹤儿子,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后砸喻,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體柔逼,經(jīng)...
    沈念sama閱讀 46,465評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,543評論 3 342
  • 正文 我和宋清朗相戀三年割岛,在試婚紗的時候發(fā)現(xiàn)自己被綠了愉适。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,675評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡癣漆,死狀恐怖维咸,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情惠爽,我是刑警寧澤癌蓖,帶...
    沈念sama閱讀 36,354評論 5 351
  • 正文 年R本政府宣布,位于F島的核電站疆股,受9級特大地震影響费坊,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜旬痹,卻給世界環(huán)境...
    茶點故事閱讀 42,029評論 3 335
  • 文/蒙蒙 一附井、第九天 我趴在偏房一處隱蔽的房頂上張望讨越。 院中可真熱鬧,春花似錦永毅、人聲如沸把跨。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,514評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽着逐。三九已至,卻和暖如春意蛀,著一層夾襖步出監(jiān)牢的瞬間耸别,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,616評論 1 274
  • 我被黑心中介騙來泰國打工县钥, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留秀姐,地道東北人。 一個月前我還...
    沈念sama閱讀 49,091評論 3 378
  • 正文 我出身青樓若贮,卻偏偏與公主長得像省有,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子谴麦,可洞房花燭夜當晚...
    茶點故事閱讀 45,685評論 2 360

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