DatePickerView.swift 4.93 KB
//
//  DatePickerView.swift
//  Twear
//
//  Created by yangbin on 2021/12/24.
//

import UIKit

class DatePickerView: UIView {
    
    typealias clickAlertClosure = (_ date: Date) -> Void
    var clickClosure: clickAlertClosure!
    
    private let bgView = UIView() //白色框
    
    private lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.font = RegularFont(20)
        label.textColor = UIColor .rgbColorFromHex(0x333333)
        label.textAlignment = .center
        return label
    }()
    
//    private let minuteArray: [Int] = {
//        var array: [Int] = []
//        for i in 0..<50 {
//            array.append((i+1)*1000)
//        }
//        return array
//    }()
    private lazy var sureBtn: UIButton = {
        let button = UIButton()
        button.addTarget(self, action: #selector(clickBtnAction(_:)), for: .touchUpInside)
        button.layer.cornerRadius = 22
        button.layer.masksToBounds = true
        button.backgroundColor = UIColor .rgbColorFromHex(0x24C789)
        button.setTitle(LocString("确定"), for: .normal)
        button.titleLabel?.font = BoldFont(17)
        button.tag = 1
        return button
    }()
    
    private lazy var cancelBtn: UIButton = {
        let button = UIButton()
        button.addTarget(self, action: #selector(clickBtnAction(_:)), for: .touchUpInside)
        button.setImage(UIImage(named: "close"), for: .normal)
        button.tag = 2
        return button
    }()

    lazy var datePicker: UIDatePicker = {
        let picker = UIDatePicker()
        picker.datePickerMode = .time
        picker.locale = .current
        if #available(iOS 13.4, *) {
            picker.preferredDatePickerStyle = .wheels
        } else {
            
        }
        return picker
    }()
    
    init(title: String, sureText: String = LocString("确定"), titleColor: UIColor = UIColor.rgbColorFromHex(0x333333)) {
        super.init(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: SCREEN_HEIGHT))
        createView(title: title, sureText: sureText, titleColor: titleColor)
    }
    

    func createView(title: String, sureText: String, titleColor: UIColor) {
        self.backgroundColor = UIColor.black.withAlphaComponent(0.1)
        
        bgView.backgroundColor = .white
        
//        self.titleColor = titleColor
        titleLabel.text = title
        sureBtn.setTitle(sureText, for: .normal)
        
        let lineView = UIView()
        lineView.backgroundColor = LineColor
        addSubview(bgView)
        bgView.addSubview(titleLabel)
        bgView.addSubview(sureBtn)
        bgView.addSubview(cancelBtn)
        bgView.addSubview(datePicker)
        
        bgView.snp.makeConstraints { (make) in
            make.left.right.bottom.equalToSuperview()
            make.height.equalTo(340)
        }
        cancelBtn.snp.makeConstraints { make in
            make.height.equalTo(78)
            make.width.equalTo(58)
            make.right.top.equalToSuperview()
        }
        
        sureBtn.snp.makeConstraints { make in
            make.bottom.equalToSuperview().offset(-50)
            make.left.equalToSuperview().offset(60)
            make.right.equalToSuperview().offset(-60)
            make.height.equalTo(44)
        }
        
        titleLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.centerY.equalTo(cancelBtn)
        }
        datePicker.snp.makeConstraints { make in
            make.top.equalTo(cancelBtn.snp_bottom)
            make.left.right.equalToSuperview()
            make.bottom.equalTo(sureBtn.snp_top)
        }
        
        
        bgView.layoutIfNeeded()
        bgView.setCorners(corners: [.topLeft, .topRight], radio: 12)
    }
    
//    @objc func dateChanged(datePicker : UIDatePicker){
//        let formatter = DateFormatter()
//        //日期样式
//        formatter.dateFormat = "yyyy年MM月dd日 HH:mm:ss"
//        print(formatter.string(from: datePicker.date))
//    }
    
    @objc func clickBtnAction(_ sender: UIButton) {
        if clickClosure != nil && sender.tag == 1 {
            clickClosure!(datePicker.date)
        }
        dismiss()
    }
    
    func show() {
        let wind = KeyWindow
        self.alpha = 0
        
        wind.addSubview(self)
        UIView.animate(withDuration: 0.25, animations: { () -> Void in
            self.alpha = 1
        })
    }
    
    @objc func dismiss() {
        UIView.animate(withDuration: 0.25, animations: { () -> Void in
            self.alpha = 0
        }, completion: { (finish) -> Void in
            if finish {
                self.removeFromSuperview()
            }
        })
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */

}