HomeRecommendView.swift 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. //
  2. // HomeRecommendView.swift
  3. // ADHTuanCan
  4. //
  5. // Created by 敖德亨 on 2023/9/30.
  6. //
  7. import UIKit
  8. class HomeRecommendView: UIView {
  9. private var scrollView : UIScrollView!
  10. var dataModel : SupplyTypeListModel?
  11. var allBtns : [UIButton] = []
  12. var runTypeAction : ((String?)->Void)?
  13. var btnW = (kSCREEN_WIDTH - 72)/3
  14. override func awakeFromNib() {
  15. super.awakeFromNib()
  16. self.backgroundColor = kTBackgroundColor
  17. self.scrollView = UIScrollView.init(frame: CGRect.zero)
  18. self.scrollView.alwaysBounceHorizontal = true
  19. self.scrollView.isScrollEnabled = true
  20. self.scrollView.showsHorizontalScrollIndicator = false
  21. self.scrollView.contentInsetAdjustmentBehavior = .never
  22. self.scrollView.backgroundColor = UIColor.white
  23. self.addSubview(self.scrollView)
  24. self.scrollView.mas_remakeConstraints { make in
  25. make?.left.right().top().bottom().mas_equalTo()(self)
  26. make?.height.offset()(36 + 40)
  27. }
  28. }
  29. func configTitle(model : SupplyTypeListModel){
  30. dataModel = model
  31. var views = scrollView.subviews
  32. views.removeAll()
  33. self.allBtns.removeAll()
  34. if let _ = model.runTypeList{
  35. for i in 0..<model.runTypeList!.count{
  36. let detail : RunTypeModel = model.runTypeList![i]
  37. let btn:UIButton = UIButton.init(type: UIButton.ButtonType.custom);//新建btn
  38. btn.frame = CGRect.init(x: 24 + (btnW + 24) * CGFloat(i), y: 20, width: btnW, height: 36)
  39. btn.selectedBackgroundColor = kThemeColor
  40. btn.layer.cornerRadius = 6
  41. btn.layer.masksToBounds = true
  42. btn.setTitle("\(detail.name ?? "")", for: UIControl.State.normal)
  43. btn.setBorderWidth("1", forState: .normal)
  44. btn.setTitleColor(UIColor.color(hex: "#1C304F"), for: .normal)
  45. btn.setTitleColor(UIColor.white, for: .selected)
  46. btn.backgroundColor = UIColor.white
  47. btn.setBorderColor(UIColor.color(hex: "#EEEEEE"), forState: .normal)
  48. btn.setBorderColor(kThemeColor, forState: .selected)
  49. btn.isSelected = false
  50. btn.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: .medium)
  51. btn.tag = i
  52. btn.addTarget(self, action: #selector(btnSelectAction(sender: )), for: .touchUpInside)
  53. if i == 0{
  54. btn.isSelected = true
  55. btn.backgroundColor = kThemeColor
  56. }
  57. self.scrollView.addSubview(btn)
  58. allBtns.append(btn)
  59. }
  60. self.createLayer()
  61. }
  62. }
  63. func createLayer(){
  64. scrollView.contentSize = CGSize.init(width: CGFloat(dataModel!.runTypeList!.count) * (24 + btnW) + 24, height: 0)
  65. }
  66. @objc private func btnSelectAction(sender : UIButton){
  67. if sender.isSelected == true{
  68. return
  69. }
  70. for item in self.allBtns {
  71. let btn : UIButton = item
  72. btn.isSelected = false
  73. btn.backgroundColor = UIColor.white
  74. }
  75. sender.isSelected = true
  76. sender.backgroundColor = kThemeColor
  77. let detail = dataModel!.runTypeList![sender.tag]
  78. if (self.runTypeAction != nil)
  79. {
  80. self.runTypeAction!(detail.id)
  81. }
  82. }
  83. }