BottomAlertView.swift 4.62 KB
//
//  ZCAlertView.swift
//  zc
//
//  Created by wyp on 2020/11/4.
//  Copyright © 2020 wyp. All rights reserved.
//

import UIKit

class BottomAlertView: UIView {
    typealias clickAlertClosure = (_ index: Int) -> Void
    var clickClosure: clickAlertClosure!
    
    
    
    let bgView = UIView() //白色框
//    let bgImageView = UIImageView()
    let textField: UITextField = UITextField()
//    let countLabel = UILabel()
    
    
    

    
    private lazy var titleLabel: UILabel = {
        let label = UILabel()
//        label.text = title
        label.font = RegularFont(13)
    
        label.textAlignment = .center
        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 = RegularFont(15)
        button.tag = 1
        return button
    }()
    


    
    init(title: String, sureText: String = LocString("确定")) {
        super.init(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: SCREEN_HEIGHT))
        createView(title: title, sureText: sureText)
    }
    
    func createView(title: 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
        
        let lineView = UIView()
        lineView.backgroundColor = LineColor
        titleLabel.text = title
        addSubview(bgView)
        bgView.addSubview(titleLabel)
        bgView.addSubview(sureBtn)
        bgView.addSubview(lineView)
//        bgView.addSubview(cancelBtn)
        
        bgView.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(23)
            make.right.equalToSuperview().offset(-23)
            make.bottom.equalToSuperview().offset(-30)
//            make.height.equalTo(120)
            
        }
        
        titleLabel.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(40)
            make.right.equalToSuperview().offset(-40)
            make.top.equalToSuperview().offset(30)
        }
        
        lineView.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(14.5)
            make.right.equalToSuperview().offset(-14.5)
            make.height.equalTo(0.5)
            make.top.equalTo(titleLabel.snp.bottom).offset(30)
        }


        sureBtn.snp.makeConstraints { (make) in
            make.top.equalTo(lineView)
            make.height.equalTo(52)
            make.left.equalToSuperview().offset(14.5)
            make.right.equalToSuperview().offset(-14.5)
            make.bottom.equalToSuperview()
        }
        

 
        
        
        sureBtn.setTitle(sureText, for: .normal)
        
    }
    
    
//    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
//        let textFieldText = textField.text ?? ""
//        let existedLength : Int = textFieldText.count
//        let selectedLength : Int = range.length
//        let replaceLength : Int = string.count
//        let pointLength : Int = existedLength - selectedLength + replaceLength
////        print(selectedLength)
//        //超过16位 就不能在输入了
//        if (pointLength > 16) {
//            return false
//        } else {
//            return true
//        }
//    }
//
//
//

    
    
    @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
    }
    */

}