123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- //
- // SBTHomeTableViewHeader.swift
- // SolarBT
- //
- // Created by weclouds on 2019/1/24.
- // Copyright © 2019 weclouds. All rights reserved.
- //
- import UIKit
- class DeviceTypeBtn: UIView {
- /*
- 1、创建UI
- 2、创建点击手势 tap
- 3、 设置点击状态
- 4、设置非选中状态
- 5、指示器的改变
- 5、点击block
- 6、传入数据方法
-
- */
-
- var deviceCount : String?{
- didSet{
- if let deviceCount = deviceCount {
- deviceCountLabel?.text = deviceCount
- }
- }
- }
-
- var deviceType : String?{
- didSet{
- if let deviceType = deviceType {
- deviceTypeLabel?.text = deviceType
- }
- }
- }
-
- //回调方法
- var tapCallback:((DeviceTypeBtn)->Void)?
-
- //是否选中 点击状态
- open var isSelected:Bool = false{
- didSet{
- if isSelected {
- log.debug("选中\(isSelected)")
- deviceTypeLabel?.textColor = UIColor(hexString: "FD8B23")
- deviceCountLabel?.textColor = UIColor(hexString: "FD8B23")
- indicator?.backgroundColor = UIColor(hexString: "FD8B23")
- }else{
- log.debug("取消选中\(isSelected)")
- deviceTypeLabel?.textColor = UIColor(hexString: "#888888")
- deviceCountLabel?.textColor = UIColor(hexString: "#222222")
- indicator?.backgroundColor = UIColor.clear
- }
- }
- }
- //设备数量
- private var deviceCountLabel :UILabel?
- //设备类型
- private var deviceTypeLabel :UILabel?
- //指示器
- private var indicator :UIView?
-
- override init(frame: CGRect) {
- super.init(frame: frame)
-
- //创建UI
- createUI()
-
- //打开手势交互
- self.isUserInteractionEnabled = true
- //创建手势
- let tap = UITapGestureRecognizer(target: self, action: #selector(tapGestrueAction))
- addGestureRecognizer(tap)
- }
-
- required init?(coder aDecoder: NSCoder) {
- fatalError("init(coder:) has not been implemented")
- }
-
- func createUI() {
- self.backgroundColor = UIColor.white
-
- indicator = UIView()
- indicator?.backgroundColor = UIColor.clear
- indicator?.layer.masksToBounds = true
- indicator?.layer.cornerRadius = 1
- addSubview(indicator!)
- indicator?.snp.makeConstraints({ (make) in
- make.bottom.equalToSuperview()
- make.centerX.equalToSuperview()
- make.width.equalTo(30)
- make.height.equalTo(2)
- })
-
- deviceTypeLabel = UILabel()
- deviceTypeLabel?.textColor = UIColor(hexString: "888888")
- deviceTypeLabel?.textAlignment = .center
- deviceTypeLabel?.font = UIFont(name: "PingFangSC-Semibold", size: 10)
- deviceTypeLabel?.text = "全部设备"
- addSubview(deviceTypeLabel!)
- deviceTypeLabel?.snp.makeConstraints({ (make) in
- make.bottom.equalTo(indicator?.snp.top ?? 0).offset(-16)
- make.centerX.equalToSuperview()
- make.height.equalTo(14)
- })
-
- deviceCountLabel = UILabel()
- deviceCountLabel?.textColor = UIColor(hexString: "222222")
- deviceCountLabel?.textAlignment = .center
- deviceCountLabel?.font = UIFont(name: "PingFang-SC-Regular", size: 25)
- deviceCountLabel?.text = "57"
- addSubview(deviceCountLabel!)
- deviceCountLabel?.snp.makeConstraints({ (make) in
- make.bottom.equalTo(deviceTypeLabel?.snp.top ?? 0).offset(-3)
- make.centerX.equalToSuperview()
- make.height.equalTo(35)
- })
- }
-
- //添加手势
- @objc func tapGestrueAction() {
- self.tapCallback!(self)
- }
- }
- class SBTHomeTableViewHeader: UIView {
- /*
- 刷新函数 : 重新赋值
- 当列表数据删除数据,重新刷新头部数据
- 删除的时候 ,全部设备改变 ,对应设备类型也跟着改变(重新赋值)
- */
-
- var Counts : [String] = ["0","0","0","0"]
-
- // 回调函数
- var selectedCallback:((Int)->Void)?
-
- var deviceCounts : [String]?
- override init(frame: CGRect) {
- super.init(frame: frame)
- createUI()
- }
-
- required init?(coder aDecoder: NSCoder) {
- fatalError("init(coder:) has not been implemented")
- }
-
- //刷新函数 ,重新赋值
- func reloadDeviceCount() {
- for i in 0..<4 {
- let btn = self.viewWithTag(667 + i) as! DeviceTypeBtn
- btn.deviceCount = deviceCounts![i]
- }
- }
-
- func createUI() {
-
-
- let types = ["全部设备","太阳能控制器","逆变器","逆变一体机"]
-
- let buttonW = (KSCREENWIDTH ) / 4 //宽
- let buttonH = 112.0
- for i in 0..<4 {
- let btnView = DeviceTypeBtn(frame: CGRect(x: (buttonW ) * CGFloat(i), y: 0, width: buttonW, height: CGFloat(buttonH)))
- btnView.tag = 667 + i
- btnView.deviceType = types[i]
- btnView.deviceCount = Counts[i]
- addSubview(btnView)
-
- if btnView.tag == 667 {
- btnView.isSelected = true
- }
- btnView.tapCallback = {(btn) in
- log.debug("点击了\(btnView.isSelected)")
- self.didSelectBtn(sender: btn)
- }
- }
- }
-
- func didSelectBtn(sender:DeviceTypeBtn) {
- let btn1 = self.viewWithTag(667) as! DeviceTypeBtn
- let btn2 = self.viewWithTag(667 + 1) as! DeviceTypeBtn
- let btn3 = self.viewWithTag(667 + 2) as! DeviceTypeBtn
- let btn4 = self.viewWithTag(667 + 3) as! DeviceTypeBtn
- btn1.isSelected = false
- btn2.isSelected = false
- btn3.isSelected = false
- btn4.isSelected = false
- sender.isSelected = true
-
- self.selectedCallback!(sender.tag - 667)
- }
- }
|