FirmwareRemindView.swift 3.84 KB
//
//  FirmwareRemindView.swift
//  HDFwear
//
//  Created by yangbin on 2022/3/16.
//

import UIKit
import SnapKit

class FirmwareRemindView: UIView {

    typealias clickAlertClosure = (_ index: Int) -> Void
    var clickClosure: clickAlertClosure!
    
    let bgView = UIView() //白色框


    private lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.font = RegularFont(15)
        label.textColor = .black
        label.numberOfLines = 0
        return label
    }()
    
    private lazy var detailLabel: UILabel = {
        let label = UILabel()
        label.font = RegularFont(13)
        label.textColor = .black
        label.numberOfLines = 0
        return label
    }()
    
    private lazy var sureBtn: UIButton = {
        let button = UIButton()
        button.addTarget(self, action: #selector(clickBtnAction(_:)), for: .touchUpInside)
        button.setTitleColor(TintColor, for: .normal)
        button.titleLabel?.font = BoldFont(15)
        button.tag = 1
        return button
    }()
    
    init(title: String, detail: String, sureText: String = LocString("确定")) {
        super.init(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: SCREEN_HEIGHT))
        createView(title: title, detail: detail, sureText: sureText)
    }
    
    func createView(title: String, detail: String, sureText: String) {
//        self.frame = CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: SCREEN_HEIGHT)
        self.backgroundColor = UIColor.black.withAlphaComponent(0.7)
        
        bgView.layer.cornerRadius = 10
        bgView.clipsToBounds = true
        bgView.backgroundColor = .white
        

        titleLabel.text = title
        detailLabel.text = detail
        addSubview(bgView)
        bgView.addSubview(titleLabel)
        bgView.addSubview(detailLabel)
        bgView.addSubview(sureBtn)
        
        bgView.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview().multipliedBy(0.92)
            make.left.equalToSuperview().offset(40)
            make.right.equalToSuperview().offset(-40)
            make.bottom.equalTo(sureBtn.snp.bottom).offset(15)
        }
        
        
        titleLabel.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(16)
            make.right.equalToSuperview().offset(-16)
            make.top.equalToSuperview().offset(12)
        }
        
        detailLabel.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(16)
            make.right.equalToSuperview().offset(-16)
            make.top.equalTo(titleLabel.snp.bottom).offset(10)
        }


        sureBtn.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(-16)
            make.top.equalTo(detailLabel.snp.bottom).offset(25)
        }
        
        
        sureBtn.setTitle(sureText, for: .normal)

    }
    
    


    
    
    @objc func clickBtnAction(_ sender: UIButton) {
        if (clickClosure != nil) {
            clickClosure!(sender.tag)
        }
        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 aDecoder: 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
    }
    */

}