EverybodyLoveView.swift 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. //
  2. // EverybodyLoveView.swift
  3. // ADHTuanCan
  4. //
  5. // Created by 敖德亨 on 2023/9/29.
  6. //
  7. import UIKit
  8. class EverybodyLoveView: UIView {
  9. /// titleScrollView
  10. @IBOutlet weak var titleScrollView: UIScrollView!
  11. /// 餐品ScrollView
  12. @IBOutlet weak var scroBackView: UIView!
  13. @IBOutlet weak var scrollBackHeight: NSLayoutConstraint!
  14. var interspace : CGFloat = 10
  15. let itemWeight : CGFloat = (kSCREEN_WIDTH - 30)/2
  16. ///早中晚typemodel
  17. var dataModel : SupplyTypeListModel?
  18. ///所有人都爱Foods
  19. var dataSource : [HomeFoodDetailModel]?
  20. var allBtns : [UIButton] = []
  21. var supplyTypeAction : ((String?)->Void)?
  22. var touchUpActionBlock : ((Int)->Void)?
  23. override func awakeFromNib() {
  24. super.awakeFromNib()
  25. self.backgroundColor = kTBackgroundColor
  26. self.titleScrollView.alwaysBounceHorizontal = true
  27. self.titleScrollView.isScrollEnabled = true
  28. self.titleScrollView.showsHorizontalScrollIndicator = false
  29. self.titleScrollView.contentInsetAdjustmentBehavior = .never
  30. self.scrollBackHeight.constant = 340
  31. self.scroBackView.addSubview(self.scrollView)
  32. self.scrollView.mas_remakeConstraints { make in
  33. make?.left.right().mas_equalTo()(self.scroBackView)
  34. make?.bottom.mas_equalTo()(self.scroBackView.mas_bottom)?.offset()
  35. make?.height.offset()(268)
  36. }
  37. }
  38. func configTitle(model : SupplyTypeListModel){
  39. dataModel = model
  40. self.lineView.isHidden = true
  41. for view in titleScrollView.subviews {
  42. view.removeFromSuperview()
  43. }
  44. self.allBtns.removeAll()
  45. self.titleScrollView.addSubview(self.lineView)
  46. if let _ = model.supplyTypeList{
  47. for i in 0..<model.supplyTypeList!.count{
  48. let detail = model.supplyTypeList![i]
  49. let btn = UIButton.init(frame: CGRect(x: CGFloat(kSCREEN_WIDTH/3) * CGFloat(i), y: 0, width: kSCREEN_WIDTH/3, height: 45))
  50. if i == 0{
  51. self.lineView.isHidden = false
  52. self.lineView.frame = CGRect(x: btn.frame.maxX - kSCREEN_WIDTH/3/2-30, y: 42.5, width: 60, height: 2.5)
  53. }
  54. btn.setTitle("\(detail.name ?? "")", for: .normal)
  55. btn.setTitleColor(UIColor.black, for: .normal)
  56. btn.setTitleColor(kThemeColor, for: .selected)
  57. btn.isSelected = false
  58. btn.tag = i
  59. btn.addTarget(self, action: #selector(titleBtnAction(sender:)), for: .touchUpInside)
  60. self.titleScrollView.addSubview(btn)
  61. self.allBtns.append(btn)
  62. }
  63. titleScrollView.contentSize = CGSize.init(width: CGFloat(dataModel!.supplyTypeList!.count) * CGFloat(kSCREEN_WIDTH/3), height: 0)
  64. }
  65. }
  66. @objc func titleBtnAction(sender : UIButton){
  67. if sender.isSelected{
  68. return
  69. }
  70. for item in allBtns {
  71. item.isSelected = false
  72. }
  73. sender.isSelected = true
  74. UIView.animate(withDuration: 0.2) {
  75. self.lineView.frame = CGRect(x: sender.frame.maxX - kSCREEN_WIDTH/3/2-30, y: 42.5, width: 60, height: 2.5)
  76. self.lineView.layoutIfNeeded()
  77. }
  78. let detail = dataModel!.supplyTypeList![sender.tag]
  79. if (self.supplyTypeAction != nil)
  80. {
  81. self.supplyTypeAction!(detail.id)
  82. }
  83. }
  84. func configMode(datList : [HomeFoodDetailModel]?){
  85. self.dataSource = datList
  86. if self.dataSource?.count == 0{
  87. self.scroBackView.isHidden = true
  88. }
  89. for view in scrollView.subviews {
  90. view.removeFromSuperview()
  91. }
  92. if let _ = datList{
  93. for i in 0..<datList!.count{
  94. let model : HomeFoodDetailModel = datList![i]
  95. let viewItem = (Bundle.main.loadNibNamed("EverybodyLoveItem", owner: nil, options: nil)?.last as? EverybodyLoveItem ?? nil)!
  96. scrollView.addSubview(viewItem)
  97. viewItem.configModel(model: model)
  98. viewItem.mas_makeConstraints { (make) in
  99. make?.left.offset()(CGFloat(interspace + (itemWeight + interspace)*CGFloat(i)))
  100. make?.top.mas_equalTo()(scrollView)?.offset()(10)
  101. make?.width.offset()(itemWeight)
  102. make?.height.offset()(248)
  103. }
  104. viewItem.touchUpActionBlock = {[weak self] in
  105. if self?.touchUpActionBlock != nil{
  106. self?.touchUpActionBlock!(i)
  107. }
  108. }
  109. }
  110. self.createLayer()
  111. }
  112. }
  113. func createLayer(){
  114. scrollView.contentSize = CGSize.init(width:CGFloat((self.dataSource?.count ?? 0)) * (itemWeight + interspace) + interspace, height: 0)
  115. }
  116. //MARK: collectionView
  117. lazy var scrollView : UIScrollView = {
  118. let scrollView = UIScrollView.init(frame: CGRect.zero)
  119. scrollView.alwaysBounceHorizontal = true
  120. scrollView.isScrollEnabled = true
  121. scrollView.showsVerticalScrollIndicator = false
  122. scrollView.showsHorizontalScrollIndicator = false
  123. scrollView.contentInsetAdjustmentBehavior = .never
  124. scrollView.backgroundColor = UIColor.color(hex: "#EDEDED")
  125. return scrollView
  126. }()
  127. //MARK: collectionView
  128. lazy var lineView : UIView = {
  129. let lineView = UIScrollView.init(frame: CGRect(x: 0, y: 42.5, width: 60, height: 2.5))
  130. lineView.backgroundColor = kThemeColor
  131. lineView.isHidden = true
  132. return lineView
  133. }()
  134. }