반응형

 

import UIKit

class BottomSheetController: UIViewController {
    
    //MARK: - properties
    let bottomSheetTableViewID = "bottomSheetTableViewID"
    let otmealColor = UIColor(red: 240/255, green: 230/255, blue: 219/255, alpha: 1.0)
    
    //Bottom sheet
    var screenWidth:CGFloat = UIScreen.main.bounds.width
    var screenHeight:CGFloat = UIScreen.main.bounds.height
    
    var originY:CGFloat!
    var bsMimicHeightMin:CGFloat!
    var bsMimicHeightMax:CGFloat!
    var isOpen:Bool = false
    var conVC:UIViewController!
  
   //MARK: - init
    convenience init() {
        self.init(controller: nil , max: 0.0 , min:0.0)
    }
    init(controller:UIViewController?,max:CGFloat,min:CGFloat) {
        self.conVC = controller
        self.bsMimicHeightMin = min
        self.bsMimicHeightMax = max
        super.init(nibName: nil, bundle: nil)
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

     //MARK: - UI
    let containerView:UIView = {
        let v = UIView()
        v.backgroundColor = .white
        v.layer.cornerRadius = 5;
        v.layer.masksToBounds = true;
        
        v.layer.shadowColor = UIColor.black.cgColor
        v.layer.shadowOpacity = 0.8
        v.layer.shadowOffset = CGSize.zero
        v.layer.shadowRadius = 3
        v.layer.masksToBounds = false
        return v
    }()
    
    let pullView:UIView = {
        let v = UIView()
        v.translatesAutoresizingMaskIntoConstraints = false
        v.backgroundColor = .lightGray
        v.layer.cornerRadius = 5;
        v.layer.masksToBounds = true;
        return v
    }()
    
    var tableView : UITableView = {
       let tableview = UITableView()
        tableview.translatesAutoresizingMaskIntoConstraints = false
        tableview.backgroundColor = .lightGray
        tableview.separatorStyle = .singleLine
        tableview.bounces = false
        return tableview
    }()
     //MARK: - viewDidLoad
    override func viewDidLoad() {
        super.viewDidLoad()
        initSelf()
        initContainer()
        initPullView()
        initTableview()
        

    }
    func initSelf(){
        view.backgroundColor = UIColor.black.withAlphaComponent(0.0)
    }
    
    func initContainer(){
        view.addSubview(containerView)
        containerView.frame = CGRect(x: 0, y: screenHeight-bsMimicHeightMin, width: screenWidth, height: bsMimicHeightMax)
        originY = containerView.frame.origin.y
        //action handler
         let tab = UIPanGestureRecognizer(target: self, action: #selector(pullViewRecognizer(pan:)))
         containerView.addGestureRecognizer(tab)
    }
    func initPullView(){
//        prepareBackgroundView(myView: pullView)
        containerView.addSubview(pullView)
        NSLayoutConstraint.activate([
            pullView.widthAnchor.constraint(equalToConstant: view.frame.size.width/3),
            pullView.heightAnchor.constraint(equalToConstant: 10),
            pullView.centerXAnchor.constraint(equalTo: containerView.centerXAnchor),
            pullView.topAnchor.constraint(equalTo: containerView.topAnchor, constant: 10)
        ])
    }
    func initTableview(){
        tableView.delegate = self
        tableView.dataSource = self
        tableView.register(MenuItemCell.self, forCellReuseIdentifier: bottomSheetTableViewID)
        
        containerView.addSubview(tableView)
        tableView.topAnchor.constraint(equalTo: pullView.bottomAnchor, constant: 30).isActive = true
        tableView.leadingAnchor.constraint(equalTo: self.containerView.leadingAnchor, constant: 0).isActive = true
        tableView.bottomAnchor.constraint(equalTo: self.containerView.bottomAnchor, constant: 0).isActive = true
        tableView.trailingAnchor.constraint(equalTo: self.containerView.trailingAnchor, constant: 0).isActive = true
    }
    
    
    func prepareBackgroundView(myView:UIView){
        let blurEffect = UIBlurEffect.init(style: .extraLight)
        let visualEffect = UIVisualEffectView.init(effect: blurEffect)
        let bluredView = UIVisualEffectView.init(effect: blurEffect)
        bluredView.contentView.addSubview(visualEffect)

        visualEffect.frame = myView.frame
        bluredView.frame = myView.frame
        myView.insertSubview(bluredView, at: 0)
    }
    
    //MARK: - action handler
    @objc func pullViewRecognizer(pan : UIPanGestureRecognizer){
        let translation = pan.translation(in: containerView)
        let velocity:CGFloat = pan.velocity(in: containerView).y
        
        if pan.state == .began {
             print("began \(translation.y)")
        }
        
        if  pan.state == .changed {
            if (isOpen){
                containerView.frame.origin.y = screenHeight-bsMimicHeightMax+translation.y
            } else {
                containerView.frame.origin.y = originY+translation.y
            }
        }
        
        if  pan.state == .ended {
            if (velocity<0){
                open()
            } else {
               close()
            }
        }
    }
    func open(){
        isOpen = true
        UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0, options: .curveEaseInOut, animations: {
            self.containerView.frame.origin.y = self.screenHeight-self.bsMimicHeightMax
            self.view.backgroundColor = UIColor.black.withAlphaComponent(0.6)
        }, completion: nil)
    }
    func close(){
        print("close")
        isOpen = false
        UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0, options: .curveEaseInOut, animations: {
            self.containerView.frame.origin.y = self.originY
            self.view.backgroundColor = UIColor.black.withAlphaComponent(0.0)
        }, completion: nil)
    }
  
}

//MARK: - tableview delegate
extension BottomSheetController : UITableViewDelegate,UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 50
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cells = tableView.dequeueReusableCell(withIdentifier: bottomSheetTableViewID, for: indexPath)
        cells.backgroundColor = .white
        return cells
    }
    
    //cell height
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 60
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        close()
    }
    
}
반응형

+ Recent posts