自定義ViewController切換動(dòng)畫

沒什么事研究了一下自定義ViewController切換動(dòng)畫,主要就是主ViewController實(shí)現(xiàn)UIViewControllerTransitioningDelegate協(xié)議中的
顯示:
optional public func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning?,
隱藏:
func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning?
兩個(gè)代理方法,在兩個(gè)代理方法中返回實(shí)現(xiàn)了UIViewControllerAnimatedTransitioning協(xié)議的兩個(gè)方法的自定義動(dòng)畫對(duì)象,閑話少絮,直接上代碼,參照官方案例寫的代碼,做了小小的改動(dòng)


//主ViewController
class ViewController: UITableViewController, UIViewControllerTransitioningDelegate{
    //.....需要在ViweDidload方法中設(shè)置navgationCroller的dalegate為主ViewController
    //tableView代理方法,點(diǎn)擊cell 使用present方式顯示控制器
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            let controller =  AniTestViewController() //要顯示的VC實(shí)例
            //設(shè)置代理為主VC
            controller.transitioningDelegate = self 
            self.present(controller, animated: true, completion: nil)
    }
    //顯示vc代理方法
    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        let aminator = MyAnimator(presenting: true)
        return aminator
    }
    //隱藏vc代理方法
    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        let animator = MyAnimator(presenting: false)
        return animator
    }
    //navgationController代理方法,如果有navgationController的話
    func navigationController(_ navigationController: UINavigationController, animationControllerFor operation: UINavigationControllerOperation, from fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        if operation == .push {
            let aminator = MyAnimator(presenting: true)
            return aminator
        }else if operation == .pop{
            let aminator = MyAnimator(presenting: false)
            return aminator
        }
        return nil;
    }
}

//實(shí)現(xiàn)UIViewControllerAnimatedTransitioning協(xié)議的類,可以是任何類
class MyAnimator: NSObject, UIViewControllerAnimatedTransitioning{
    //在初始化方法中設(shè)置初始值,用以判斷是顯示還是隱藏vc
    var presenting:Bool = true
    
    init(presenting: Bool) {
        super.init()
        self.presenting = presenting
    }
    
    //代理方法,返回動(dòng)畫執(zhí)行時(shí)間
    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        return 0.5
    }
    
    //動(dòng)畫執(zhí)行方法,用以寫動(dòng)畫執(zhí)行過程
    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        //通過transitionContext獲取用來執(zhí)行動(dòng)畫的View
        let containerView = transitionContext.containerView
        //要從哪個(gè)vc跳轉(zhuǎn),比如在A_ViewController中要顯示B_ViewController, fromVC就是A
        let fromVC = transitionContext.viewController(forKey: .from)
        //要從跳轉(zhuǎn)到哪個(gè)VC,比如在A_ViewController中要顯示B_ViewController, toVC就是B
        let toVC = transitionContext.viewController(forKey: .to)
        //如果已經(jīng)在A中執(zhí)行過present,要dismiss掉B的時(shí)候,那么A為toVC,B為fromVC

        //toView與fromView可以按toVC和fromVC的方式理解,也就是即將要顯示的那個(gè)View叫做toView,現(xiàn)在顯示的View叫做fromView
        let toView = transitionContext.view(forKey: .to)
        let fromView = transitionContext.view(forKey: .from)
        
        //獲取動(dòng)畫的各個(gè)frame
        //containerFrame,動(dòng)畫執(zhí)行所在的區(qū)域,也就是ScreenBounds
        let containerFrame = containerView.frame
        //要顯示的VC開始所在的位置大小,默認(rèn)(x:0, y:0, width:0, Height:0)
        var toViewStartFrame = transitionContext.initialFrame(for: toVC!)
        //要顯示的VC最終要移動(dòng)到的位置大小,默認(rèn)(x:0, y:0, width:ScreenWidth, Height:ScreenHeight)
        let toViewFinalFrame = transitionContext.finalFrame(for: toVC!)
        //現(xiàn)在顯示的VC最終的位置大小,默認(rèn)(x:0, y:0, width:0, Height:0)
        var fromViewFinalFrame = transitionContext.finalFrame(for: fromVC!)
        
        containerView.addSubview(toView!)
        //判斷是顯示還是隱藏
        if self.presenting{
            //顯示
            //設(shè)置 toViewStartFrame用以初始化要顯示VC的初始位置
            toViewStartFrame.origin.x = containerFrame.size.width
            toViewStartFrame.origin.y = containerFrame.size.height
            toView?.frame = toViewStartFrame
        }else{
            //如果是隱藏
            //設(shè)置現(xiàn)在顯示的VC動(dòng)畫結(jié)束的位置
            fromViewFinalFrame = CGRect.init(x: containerFrame.size.width, y: containerFrame.size.height, width:(toView?.frame.size.width)!, height:(toView?.frame.size.height)!);
            //設(shè)置要顯示View的frame為當(dāng)前顯示的View的位置,并將其移動(dòng)到但錢view的下面
            toView?.frame = toViewFinalFrame
            containerView.bringSubview(toFront: fromView!)
        }
        
        //執(zhí)行動(dòng)畫
        UIView.animate(withDuration: self.transitionDuration(using:transitionContext), animations: {
            if self.presenting{
                //如果是顯示的話那么就設(shè)置要顯示的View的動(dòng)畫結(jié)束位置,UIView會(huì)自動(dòng)執(zhí)行動(dòng)畫
                toView?.frame = toViewFinalFrame
            }else{
                //移除的時(shí)候設(shè)置現(xiàn)在顯示的View的動(dòng)畫結(jié)束位置
                fromView?.frame = fromViewFinalFrame
            }
            //當(dāng)然,如果你愿意的話可以自己設(shè)置各種各樣的動(dòng)畫效果,以滿足需求
        }) { (finshed) in
            //動(dòng)畫結(jié)束如果失敗的話則需要將要顯示的view移除掉
            let success = !transitionContext.transitionWasCancelled
            if self.presenting && !success || !self.presenting && success{
                toView?.removeFromSuperview()
            }
            //告訴UIKit動(dòng)畫執(zhí)行結(jié)束了,以執(zhí)行之后的操作
            transitionContext.completeTransition(success)
        }
    }
    
    
}

動(dòng)畫效果就是這個(gè)樣子的:::

自定義動(dòng)畫

示例代碼:https://github.com/HiGodl/ViewControllerChangeAnim

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容