swift 給一個(gè)modal出來的控制器設(shè)置轉(zhuǎn)場動(dòng)畫【轉(zhuǎn)載XMG】

1. 設(shè)置控制器屬性

        // 設(shè)置modal樣式,彈出后原來的view不會(huì)消失,默認(rèn)modal出來的控制器view會(huì)將原來的view移除
        popoverVc.modalPresentationStyle = .custom
        // 設(shè)置轉(zhuǎn)場代理
        popoverVc.transitioningDelegate = self

2. 設(shè)置轉(zhuǎn)場代理方法

// MARK:- transitioningDelegate代理
extension GGHomeViewController : UIViewControllerTransitioningDelegate {
    /// 目的:改變彈出view的frame憋槐,需要自定義UIPresentationController
    func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {
        return GGPresentationController(presentedViewController: presented, presenting: presenting)
    }
    
    /// 目的:設(shè)置彈出動(dòng)畫
    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresented = true // 由于彈出和消失用的是同一個(gè)協(xié)議婆殿,所需需要設(shè)置一個(gè)標(biāo)志位來區(qū)分是彈出還是消失
        return self
    }
    /// 目的:設(shè)置消失動(dòng)畫
    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresented = false
        return self
    }
}
2.1 自定義UIPresentationController
class GGPresentationController: UIPresentationController {
    
    // MARK:- 懶加載屬性
    fileprivate lazy var coverView : UIView = UIView()
    
    // MARK:- 系統(tǒng)函數(shù)回調(diào)
    override func containerViewWillLayoutSubviews() {
        super.containerViewWillLayoutSubviews()
        
        // 設(shè)置frame
        presentedView?.frame = CGRect(x: 100, y: 55, width: 180, height: 250)
        
        setupCoverView()
    }
}

// MARK:- 設(shè)置蒙版
extension GGPresentationController {
    fileprivate func setupCoverView() {
        // 1. 添加蒙版
        containerView?.insertSubview(coverView, at: 0)
        
        // 2. 設(shè)置屬性
        coverView.frame = containerView!.bounds
        coverView.backgroundColor = UIColor(white: 0.6, alpha: 0.2)
        
        // 3. 給蒙版添加手勢事件
        let tabGes = UITapGestureRecognizer(target: self, action: #selector(GGPresentationController.coverViewTapGesClk))
        coverView.addGestureRecognizer(tabGes)
    }
}

// MARK:- 手勢點(diǎn)擊處理
extension GGPresentationController {
    @objc fileprivate func coverViewTapGesClk() {
        print("coverViewTapGesClk")
        presentedViewController.dismiss(animated: true, completion: nil)
    }
}

3. 設(shè)置彈出载矿、消失動(dòng)畫的協(xié)議

// MARK:- UIViewControllerAnimatedTransitioning協(xié)議
extension GGHomeViewController : UIViewControllerAnimatedTransitioning {
    /// 設(shè)置動(dòng)畫播放事件
    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        return 0.5
    }
    
    /// 設(shè)置動(dòng)畫效果
    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        isPresented ? animationToPresentedView(using: transitionContext) : animationToDismissedView(using: transitionContext)
    }
    
    // 彈出動(dòng)畫
    fileprivate func animationToPresentedView(using transitionContext: UIViewControllerContextTransitioning) {
        // 1. 獲取彈出的view
        let presentedView = transitionContext.view(forKey: UITransitionContextViewKey.to)!
        
        // 2. 添加view
        transitionContext.containerView.addSubview(presentedView)
        
        // 3. 設(shè)置動(dòng)畫
        presentedView.transform = CGAffineTransform(scaleX: 1.0, y: 0.0)
        presentedView.layer.anchorPoint = CGPoint(x: 0.5, y: 0) // 動(dòng)畫的開始位置
        UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {
            presentedView.transform = CGAffineTransform.identity
        }) { (_) in
            // 4. 完成動(dòng)畫
            transitionContext.completeTransition(true)
        }
    }
    
    // 消失動(dòng)畫
    // 返回的對(duì)象是一個(gè)協(xié)議板辽,直接設(shè)置成當(dāng)前對(duì)象
    fileprivate func animationToDismissedView(using transitionContext: UIViewControllerContextTransitioning) {
        // 1. 獲取彈出的view
        let presentedView = transitionContext.view(forKey: UITransitionContextViewKey.from)!
        
        // 2. 設(shè)置動(dòng)畫
        UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {
            presentedView.transform = CGAffineTransform(scaleX: 1.0, y: 0.001)
        }) { (_) in
            // 3. 完成動(dòng)畫
            transitionContext.completeTransition(true)
        }
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末巷挥,一起剝皮案震驚了整個(gè)濱河市桩卵,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌倍宾,老刑警劉巖雏节,帶你破解...
    沈念sama閱讀 217,657評(píng)論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異高职,居然都是意外死亡钩乍,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,889評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門初厚,熙熙樓的掌柜王于貴愁眉苦臉地迎上來件蚕,“玉大人,你說我怎么就攤上這事产禾∨抛鳎” “怎么了?”我有些...
    開封第一講書人閱讀 164,057評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵亚情,是天一觀的道長妄痪。 經(jīng)常有香客問我,道長楞件,這世上最難降的妖魔是什么衫生? 我笑而不...
    開封第一講書人閱讀 58,509評(píng)論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮土浸,結(jié)果婚禮上罪针,老公的妹妹穿的比我還像新娘。我一直安慰自己黄伊,他們只是感情好泪酱,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,562評(píng)論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般墓阀。 火紅的嫁衣襯著肌膚如雪毡惜。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,443評(píng)論 1 302
  • 那天斯撮,我揣著相機(jī)與錄音经伙,去河邊找鬼。 笑死勿锅,一個(gè)胖子當(dāng)著我的面吹牛帕膜,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播溢十,決...
    沈念sama閱讀 40,251評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼泳叠,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了茶宵?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,129評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤宗挥,失蹤者是張志新(化名)和其女友劉穎乌庶,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體契耿,經(jīng)...
    沈念sama閱讀 45,561評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡瞒大,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,779評(píng)論 3 335
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了搪桂。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片透敌。...
    茶點(diǎn)故事閱讀 39,902評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖踢械,靈堂內(nèi)的尸體忽然破棺而出酗电,到底是詐尸還是另有隱情,我是刑警寧澤内列,帶...
    沈念sama閱讀 35,621評(píng)論 5 345
  • 正文 年R本政府宣布撵术,位于F島的核電站,受9級(jí)特大地震影響话瞧,放射性物質(zhì)發(fā)生泄漏嫩与。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,220評(píng)論 3 328
  • 文/蒙蒙 一交排、第九天 我趴在偏房一處隱蔽的房頂上張望划滋。 院中可真熱鬧,春花似錦埃篓、人聲如沸处坪。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,838評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽稻薇。三九已至嫂冻,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間塞椎,已是汗流浹背桨仿。 一陣腳步聲響...
    開封第一講書人閱讀 32,971評(píng)論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留案狠,地道東北人服傍。 一個(gè)月前我還...
    沈念sama閱讀 48,025評(píng)論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像骂铁,于是被迫代替她去往敵國和親吹零。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,843評(píng)論 2 354

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