CNLiveSelectCouponTapView.swift 3.19 KB
//
//  CNLiveSelectCouponTapView.swift
//  metaCode
//
//  Created by zx on 2023/12/15.
//

import Foundation
typealias selectTypeActionBlock = (_ section:Int) -> Void

class CNLiveSelectCouponTapView:UIView{
    var _titles:Array<String>?
    var titles:Array<String>?{
        get{
            _titles
        }
        set{
            _titles = newValue
            if _titles?.count ?? 0 == 0{
                return
            }
            for (idx,obj) in _titles!.enumerated(){
                let btn = UIButton(type: .custom)
                btn.setTitleColor(HexColor("#666666"), for: .normal)
                btn.setTitle(obj, for: .normal)
                btn.setImage(UIImage(named: "DS_coupon_unSelect"), for: .normal)
                btn.setImage(UIImage(named: "DS_coupon_select"), for: .selected)
                btn.titleLabel?.font = Font_15
                btn.tag = 1001+idx
                btn.addTarget(self, action: #selector(selectIndex(_:)), for: .touchUpInside)
                self.addSubview(btn)
                
                self.btnArray.add(btn)
            }
            let btn1 = self.btnArray[1] as! UIButton
            let btn2 = self.btnArray[0] as! UIButton
            var width1:CGFloat = 0
            if titles![1].count == 4{
                width1 = 83
            }else if titles![1].count == 3{
                width1 = 68
            }else if titles![1].count == 2{
                width1 = 53
            }
            btn1.snp.makeConstraints { make in
                make.right.equalTo(self)
                make.width.equalTo(width1)
                make.height.equalTo(self)
            }
            var width2:CGFloat = 0
            if titles![0].count == 4{
                width2 = 83
            }else if titles![0].count == 3{
                width2 = 68
            }else if titles![0].count == 2{
                width2 = 53
            }
            btn2.snp.makeConstraints { make in
                make.right.equalTo(btn1.snp.left).offset(-30)
                make.width.equalTo(width2)
                make.height.equalTo(self)
            }
        }
    }
    
    var _selectIndex:Int = 0
    var selectIndex:Int{
        get{
            _selectIndex
        }
        set{
            _selectIndex = newValue
            for button in self.btnArray{
                let btn = button as! UIButton
                if btn.tag - 1000 == selectIndex{
                    btn.isSelected = true
                }else{
                    btn.isSelected = false
                }
            }
        }
    }
    
    var editEnabled:Bool = false
    var selectTypeBlock:selectTypeActionBlock?
    lazy var btnArray :NSMutableArray = {
        let btnArray = NSMutableArray()
        return btnArray
    }()
    @objc func selectIndex(_ sender:UIButton){
        if self.editEnabled == false{
            showMessage(message: "该项不可编辑")
            return
        }
        for btn in self.btnArray{
            let button = btn as! UIButton
            if button.tag == sender.tag{
                button.isSelected = true
                self.selectTypeBlock!(sender.tag - 1000)
            }else{
                button.isSelected = false
            }
        }
    }
    
}