自定義轉(zhuǎn)場動畫

1. 自定義動畫管理類
//
//  PresentTransitionAnimated.swift
//  Bitwin
//
//  Created by HarrySun on 2018/9/6.
//  Copyright ? 2018年 HarrySun. All rights reserved.
//

import UIKit

/// present動畫類
class PresentTransitionAnimated: NSObject, UIViewControllerAnimatedTransitioning {
    
    var duration = 0.2
    
    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        return duration
    }
    
    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        
        // 得到容器視圖
        let containerView = transitionContext.containerView
        // 目標(biāo)視圖
        guard let toViewController = transitionContext.viewController(forKey: .to) as? GuessBuyViewController else { return }
        let toView = toViewController.baseView
        containerView.addSubview(toView)
        
        toView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        toView.bgView.snp.updateConstraints({ (make) in
            make.left.right.equalToSuperview()
            make.bottom.equalToSuperview().offset(EXHelper.screenWidth * 1.32)
        })
        containerView.layoutIfNeeded()
        
        UIView.animate(withDuration: duration, animations: {
            toView.bgView.snp.remakeConstraints({ (make) in
                make.bottom.equalToSuperview()
                make.left.right.equalToSuperview()
            })
            containerView.layoutIfNeeded()
        }, completion: { (_) in
            transitionContext.completeTransition(true)
        })
    }
}

/// dismiss動畫類
class DismissTransitionAnimated: NSObject, UIViewControllerAnimatedTransitioning {
    
    var duration = 0.2
    
    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        return duration
    }
    
    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        
        // 得到容器視圖
        let containerView = transitionContext.containerView
        // 目標(biāo)視圖
        guard let fromViewController = transitionContext.viewController(forKey: .from) as? GuessBuyViewController else { return }
        let fromView = fromViewController.baseView
        containerView.addSubview(fromView)
        
        UIView.animate(withDuration: duration, animations: {
            fromView.bgView.snp.remakeConstraints({ (make) in
                make.top.equalTo(EXHelper.screenHeight)
                make.left.right.equalToSuperview()
            })
            containerView.layoutIfNeeded()
        }, completion: { (_) in
            transitionContext.completeTransition(true)
        })
    }
}
2. VC中使用,需要設(shè)置代理
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        let vc = TargetViewController()
        vc.transitioningDelegate = self
        MainTabBarController.main?.present(vc, animated: true, completion: nil)
    }

3. 實現(xiàn)代理方法

extension TargetViewController: UIViewControllerTransitioningDelegate {
    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return PresentTransitionAnimated()
    }

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

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

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