ios swift 从下往上推出 viewcontroller 里的View

389 阅读1分钟

思路 parentController 加 childController 的 view

直接上源码



//  PPBottomupController.swift

import UIKit

  


/// 从下往上推出

class PPBottomupController: UIViewController {

    /// 子类 可以重写

    public var contentHeight:Int = 370

    

    private(set) var isShowed: Bool = false

    

//    var presentingController:UIViewController?

    

    lazy var bgTouchView: UIView  = {

        let view = UIView()

        view.backgroundColor = UIColor.colorWithHexString("#000000",alpha: 0.5)

        return view

    }()

    

    /// 从下往上推出的view  需要加在 contentView 上

    lazy var contentView: UIView  = {

        let view = UIView(frame:

                            CGRect(x: 0, y: kScreenHeight,

                                   width: kScreenWidth, height: CGFloat(self.contentHeight)))

        return view

    }()

  


    override func viewDidLoad() {

        super.viewDidLoad()

        self.addSubviews()

        

        let tapGesutre = UITapGestureRecognizer {[weak self] _ in

            

            guard let self = self else { return }

            self.hide()

        }

        self.bgTouchView.addGestureRecognizer(tapGesutre)

  


    }

    

    private func addSubviews()  {

        self.view.addSubview(bgTouchView)

        bgTouchView.snp.makeConstraints { make in

            make.edges.equalToSuperview()

        }

        

        self.view.addSubview(self.contentView)

        self.contentView.snp.makeConstraints { make in

            make.leading.trailing.bottom.equalToSuperview()

            make.height.equalTo(self.contentHeight)

        }

    }

  


    func show(in presentingController: UIViewController, completion: (() -> Void)? = nil) {

        self.isShowed = true

        guard let presentingView = presentingController.view else {

            return

            

        }

//        self.presentingController = presentingController

        if presentingView.subviews.contains(self.view){

            presentingView.bringSubviewToFront(self.view)

        }else {

            presentingController.addChild(self)

            presentingView.addSubview(self.view)

        }

        self.view.isHidden = false

        UIView.animate(withDuration: 0.3, delay: 0, options: .curveEaseOut, animations: {

            self.contentView.frame.origin.y = kScreenHeight - CGFloat(self.contentHeight)

            self.bgTouchView.alpha = 1

        }) { (success) in

            completion?()

        }

    }

    

    func hide(completion: ((Bool) -> Void)? = nil){

        self.isShowed = false

  


        UIView.animate(withDuration: 0.3, delay: 0, options: .curveEaseOut, animations: {

            self.contentView.frame.origin.y = kScreenHeight

            self.bgTouchView.alpha = 0

        }) { (success) in

            self.view.isHidden = true

            completion?(success)

        }

    }

    

    func dismiss() {

        if self.view.superview == nil { return }

        self.isShowed = false

        

        self.hide {[weak self] success in

            guard let self = self else {

                return

            }

            self.view.removeFromSuperview()

        }

        self.removeFromParent()

    }

  


}