CNLiveLoginPickerView.swift 6.01 KB
//
//  CNLiveLoginPickerView.swift
//  metaCode
//
//  Created by open on 2023/6/5.
//

import Foundation

typealias CloseClickResultBlock = (_ text: String) ->Void

class CNLiveLoginPickerView : UIView, UIPickerViewDelegate,UIPickerViewDataSource {
    
    lazy var tapGesture = UITapGestureRecognizer()
    lazy var grayMaskView: UIView = {
       let grayMaskView = UIView()
       tapGesture.addTarget(self, action: #selector(setupCloseButtonAction))
       grayMaskView.backgroundColor = UIColor.black.withAlphaComponent(0.2)
       grayMaskView.addGestureRecognizer(tapGesture)
       return grayMaskView
    }()
    
    lazy var backView: UIView = {
        let backView = UIView()
        backView.backgroundColor = UIColor.white
        backView.layer.cornerRadius = 12
        return backView
    }()

    lazy var pickerView : UIPickerView = {
        let pickerView = UIPickerView.init()
        pickerView.backgroundColor = .white
        pickerView.showsSelectionIndicator = true
        pickerView.delegate = self
        pickerView.dataSource = self
        return pickerView
    }()
    
    lazy var lineLable : UILabel = {
        let lineLable = UILabel.init()
        lineLable.backgroundColor = HexColor("#E6E6E6")
        return lineLable
    }()
    
    lazy var doneButton : UIButton = {
        let doneButton = UIButton(type: .custom)
        doneButton.setTitle("确定", for: .normal)
        doneButton.setTitleColor(HexColor("#0066FE"), for: .normal)
        doneButton.titleLabel?.font = Font_16
        doneButton.addTarget(self, action: #selector(setupDoneButtonAction), for: .touchUpInside)
        return doneButton
    }()
    
    lazy var closeButton : UIButton = {
        let closeButton = UIButton(type: .custom)
        closeButton.setImage(UIImage(named: "login_alert_country_close"), for: .normal)
        closeButton.backgroundColor = .clear
        closeButton.addTarget(self, action: #selector(setupCloseButtonAction), for: .touchUpInside)
        return closeButton
    }()
    
    lazy var country_array : NSArray = {
        let array = NSArray(contentsOfFile: Bundle.main.path(forResource: "countries", ofType: "plist") ?? "")!
        return array
    }()
    
    var resutBlock : CloseClickResultBlock?
    var result_string : String = ""
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        addSubview(grayMaskView)
        addSubview(backView)
        backView.addSubview(lineLable)
        backView.addSubview(pickerView)
        backView.addSubview(closeButton)
        backView.addSubview(doneButton)
        
        setConstraints()
    }
    
    func setConstraints() {
        grayMaskView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        backView.snp.makeConstraints { make in
            make.top.equalTo(self.snp.bottom).offset(0)
            make.left.right.equalToSuperview()
        }
        var safeBottomHeight: CGFloat = 0
        if #available(iOS 11.0, *) {
            safeBottomHeight = UIApplication.shared.windows.last?.safeAreaInsets.bottom ?? 0
        } else {
            // Fallback on earlier versions
        }
        pickerView.snp.makeConstraints { make in
            make.bottom.equalTo(-safeBottomHeight-20)
            make.left.right.equalToSuperview()
            make.top.equalTo(50)
        }
        doneButton.snp.makeConstraints { make in
            make.right.equalToSuperview()
            make.top.equalTo(10)
            make.height.equalTo(30)
            make.width.equalTo(60)
        }
        closeButton.snp.makeConstraints { make in
            make.centerY.equalTo(doneButton)
            make.left.equalTo(8)
            make.size.equalTo(30)
        }
        lineLable.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.height.equalTo(1)
            make.top.equalTo(pickerView.snp.top).offset(-1)
        }
    }
    
    
    
    static public func showSelectView(resultBlock: @escaping CloseClickResultBlock){
        let pickerView = CNLiveLoginPickerView()
        pickerView.resutBlock = resultBlock
        
        let window = UIApplication.shared.windows.last
        window?.addSubview(pickerView)
        pickerView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        window?.layoutIfNeeded()
        pickerView.alpha = 0
        UIView.animate(withDuration: 0.1) {
            pickerView.alpha = 1
        } completion: { _ in
            UIView.animate(withDuration: 0.1) {
                pickerView.backView.snp.updateConstraints { make in
                    make.top.equalTo(pickerView.snp.bottom).offset(-pickerView.backView.frame.height)
                }
                pickerView.layoutIfNeeded()
            }
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    deinit {
        
    }
}

extension CNLiveLoginPickerView {
    
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }
    
    
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return country_array.count
    }
    
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        let param = country_array[row] as! NSDictionary
        let name = param["name"] as? String
        let code = param["code"] as? String
        return "\(name ?? "")"+"(+\(code ?? ""))"
    }
    
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        let param = country_array[row] as! NSDictionary
        result_string = param["code"] as! String
    }
}

extension CNLiveLoginPickerView {
    
    @objc func setupDoneButtonAction(){
        if let resutBlock = resutBlock {
            resutBlock(result_string)
        }
        disMissView()
    }
    @objc func setupCloseButtonAction() -> Void{
        disMissView()
    }
    func disMissView() {
        resutBlock = nil
        removeFromSuperview()
    }

}