JLChen
2021-05-18 a869383e163a18cdedcf587383c1eca043129754
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
//
//  Copyright © 2018年 Zhejiang Dahua Technology Co.,Ltd. All rights reserved.
//    网络连接超时的按钮
 
import UIKit
 
 
enum DHLightButtonType {
    case redConstant
    case redRotate
    case redTwinkling
    case greenConstant
    case greenTwinkling
    case yellowTwinkling
    case blueConstant
    case greenBlueConstant
    case qrCodeBtn
    
    func animateType() -> DHLightButtonAnimateType {
        var type: DHLightButtonAnimateType = .constant
        switch self {
        case .redTwinkling, .greenTwinkling, .yellowTwinkling:
            type = .twinkling
        
        case .redRotate:
            type = .rotate
            
        default:
            type = .constant
        }
        
        return type
    }
    
    func color() -> UIColor {
        var color = UIColor.orange
        color = UIColor.dhcolor_c1()
        switch self {
        case .greenConstant, .greenTwinkling, .greenBlueConstant:
            color = UIColor.dhcolor_c11()
        case .yellowTwinkling:
            color = UIColor.dhcolor_c33()
        case .blueConstant:
            color = UIColor.dhcolor_c32()
        case .qrCodeBtn:
            color = UIColor.dhcolor_c5()
        default:
            color = UIColor.dhcolor_c12()
        }
        
        return color
    }
    
    func bgColor() -> UIColor {
        
        var color = UIColor.clear
        if self == .qrCodeBtn {
            color = UIColor.dhcolor_c1()
        }
        return color
    }
    
    func titleColor() -> UIColor {
        
        var color = UIColor.black
        color = UIColor.dhcolor_c2()
        if self == .qrCodeBtn {
            color = UIColor.dhcolor_c43()
        } else {
            color = self.color()
        }
        return color
    }
    
    func borderColor() -> UIColor {
        
        var color = UIColor.black
        color = UIColor.dhcolor_c2()
        if self == .qrCodeBtn {
            color = UIColor.clear
        } else {
            color = self.color()
        }
        return color
    }
    
    func imageNameAndTitle() -> (imageName: String, title: String) {
        switch self {
        case .redTwinkling:
            return ("adddevice_light_redflash", "add_device_red_light_twinkle".lc_T)
        case .redRotate:
            return ("adddevice_light_redflash", "add_device_red_light_rotate".lc_T)
        case .greenConstant:
            return ("adddevice_light_greenalways", "add_device_green_light_always".lc_T)
        case .greenTwinkling:
            return ("adddevice_light_greenflash", "add_device_green_light_twinkle".lc_T)
        case .yellowTwinkling:
            return ("adddevice_light_yellowflash", "add_device_yellow_light_twinkle".lc_T)
        case .greenBlueConstant:
            return ("adddevice_light_greenalways", "add_device_green_blue_light_always".lc_T)
        case .blueConstant:
            return ("adddevice_light_bluealways", "add_device_blue_light_always".lc_T)
        case .qrCodeBtn:
            return ("adddevice_icon_code", "add_device_timeout_add_by_qrcode".lc_T)
        default:
            return ("adddevice_light_redalways", "add_device_red_light_always".lc_T)
        }
    }
}
 
/// 指示灯动画类型
enum DHLightButtonAnimateType {
    case constant    //常亮
    case twinkling    //闪烁
    case rotate        //旋转
}
 
class DHLightButton: UIButton {
 
    /// 指示灯类型
    public var lightType: DHLightButtonType = .redConstant {
        didSet {
            layer.borderColor = lightType.color().cgColor
            autoSet()
            startAnimation()
            setTitleColor(lightType.titleColor(), for: .normal)
            self.backgroundColor = lightType.bgColor()
            layer.borderColor = lightType.borderColor().cgColor
        }
    }
    
    /// 设置灯图片
    public var dotImage: UIImage? {
        didSet {
            dotImageView.image = dotImage
        }
    }
    
    private var dotImageView: UIImageView!
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupSubviews()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        setupSubviews()
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        layer.cornerRadius = DHModuleConfig.shareInstance().commonButtonCornerRadius()
    }
    
    func setupSubviews() {
        layer.cornerRadius = DHModuleConfig.shareInstance().commonButtonCornerRadius()
        layer.borderColor = lightType.borderColor().cgColor
        backgroundColor = lightType.bgColor()
        layer.borderWidth = 0.5
        layer.masksToBounds = true
        
        
        dotImageView = UIImageView()
        addSubview(dotImageView)
        
        dotImageView.snp.makeConstraints { make in
            make.left.equalTo(20)
            make.centerY.equalTo(self)
            make.height.width.equalTo(25)
        }
        
        titleLabel?.numberOfLines = 2
    }
    
    // MARK: Animation
    public func startAnimation() {
//        if lightType.animateType() == .twinkling {
//            let animation = CABasicAnimation(keyPath: "opacity")
//            animation.fromValue = 1
//            animation.toValue = 0
//            animation.autoreverses = true
//            animation.duration = 1
//            animation.repeatCount = MAXFLOAT
//            animation.isRemovedOnCompletion = false
//            animation.fillMode = kCAFillModeForwards
//            animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseIn)
//            dotImageView.layer.add(animation, forKey: "BreathLight")
//        }
    }
    
    public func stopAnimation() {
        dotImageView.layer.removeAllAnimations()
    }
    
    // MARK: AutoSetImage
    public func autoSet() {
        setTitleColor(lightType.color(), for: .normal)
        setTitle(lightType.imageNameAndTitle().title, for: .normal)
        dotImageView.image = UIImage(named: lightType.imageNameAndTitle().imageName)
    }
}