CNLiveSelectCouponTapView.swift
3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
//
// 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
}
}
}
}