123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 |
- //
- // EverybodyLoveView.swift
- // ADHTuanCan
- //
- // Created by 敖德亨 on 2023/9/29.
- //
- import UIKit
- class EverybodyLoveView: UIView {
-
- /// titleScrollView
- @IBOutlet weak var titleScrollView: UIScrollView!
- /// 餐品ScrollView
- @IBOutlet weak var scroBackView: UIView!
-
- @IBOutlet weak var scrollBackHeight: NSLayoutConstraint!
-
- var interspace : CGFloat = 10
-
- let itemWeight : CGFloat = (kSCREEN_WIDTH - 30)/2
-
- ///早中晚typemodel
- var dataModel : SupplyTypeListModel?
-
- ///所有人都爱Foods
- var dataSource : [HomeFoodDetailModel]?
-
- var allBtns : [UIButton] = []
-
- var supplyTypeAction : ((String?)->Void)?
- var touchUpActionBlock : ((Int)->Void)?
-
- override func awakeFromNib() {
- super.awakeFromNib()
- self.backgroundColor = kTBackgroundColor
-
- self.titleScrollView.alwaysBounceHorizontal = true
- self.titleScrollView.isScrollEnabled = true
- self.titleScrollView.showsHorizontalScrollIndicator = false
- self.titleScrollView.contentInsetAdjustmentBehavior = .never
-
- self.scrollBackHeight.constant = 340
- self.scroBackView.addSubview(self.scrollView)
-
- self.scrollView.mas_remakeConstraints { make in
- make?.left.right().mas_equalTo()(self.scroBackView)
- make?.bottom.mas_equalTo()(self.scroBackView.mas_bottom)?.offset()
- make?.height.offset()(268)
- }
- }
-
-
-
- func configTitle(model : SupplyTypeListModel){
-
- dataModel = model
-
- self.lineView.isHidden = true
- for view in titleScrollView.subviews {
- view.removeFromSuperview()
- }
- self.allBtns.removeAll()
-
- self.titleScrollView.addSubview(self.lineView)
-
- if let _ = model.supplyTypeList{
- for i in 0..<model.supplyTypeList!.count{
- let detail = model.supplyTypeList![i]
-
- let btn = UIButton.init(frame: CGRect(x: CGFloat(kSCREEN_WIDTH/3) * CGFloat(i), y: 0, width: kSCREEN_WIDTH/3, height: 45))
- if i == 0{
- self.lineView.isHidden = false
- self.lineView.frame = CGRect(x: btn.frame.maxX - kSCREEN_WIDTH/3/2-30, y: 42.5, width: 60, height: 2.5)
- }
-
- btn.setTitle("\(detail.name ?? "")", for: .normal)
- btn.setTitleColor(UIColor.black, for: .normal)
- btn.setTitleColor(kThemeColor, for: .selected)
- btn.isSelected = false
- btn.tag = i
- btn.addTarget(self, action: #selector(titleBtnAction(sender:)), for: .touchUpInside)
- self.titleScrollView.addSubview(btn)
- self.allBtns.append(btn)
- }
- titleScrollView.contentSize = CGSize.init(width: CGFloat(dataModel!.supplyTypeList!.count) * CGFloat(kSCREEN_WIDTH/3), height: 0)
- }
- }
-
- @objc func titleBtnAction(sender : UIButton){
-
- if sender.isSelected{
- return
- }
-
- for item in allBtns {
- item.isSelected = false
- }
-
- sender.isSelected = true
-
- UIView.animate(withDuration: 0.2) {
- self.lineView.frame = CGRect(x: sender.frame.maxX - kSCREEN_WIDTH/3/2-30, y: 42.5, width: 60, height: 2.5)
- self.lineView.layoutIfNeeded()
- }
-
-
- let detail = dataModel!.supplyTypeList![sender.tag]
-
- if (self.supplyTypeAction != nil)
- {
- self.supplyTypeAction!(detail.id)
- }
-
- }
-
-
-
-
-
- func configMode(datList : [HomeFoodDetailModel]?){
- self.dataSource = datList
- if self.dataSource?.count == 0{
- self.scroBackView.isHidden = true
- }
- for view in scrollView.subviews {
- view.removeFromSuperview()
- }
-
- if let _ = datList{
- for i in 0..<datList!.count{
- let model : HomeFoodDetailModel = datList![i]
-
- let viewItem = (Bundle.main.loadNibNamed("EverybodyLoveItem", owner: nil, options: nil)?.last as? EverybodyLoveItem ?? nil)!
- scrollView.addSubview(viewItem)
- viewItem.configModel(model: model)
- viewItem.mas_makeConstraints { (make) in
- make?.left.offset()(CGFloat(interspace + (itemWeight + interspace)*CGFloat(i)))
- make?.top.mas_equalTo()(scrollView)?.offset()(10)
- make?.width.offset()(itemWeight)
- make?.height.offset()(248)
-
- }
- viewItem.touchUpActionBlock = {[weak self] in
- if self?.touchUpActionBlock != nil{
- self?.touchUpActionBlock!(i)
- }
- }
- }
- self.createLayer()
- }
- }
-
- func createLayer(){
-
- scrollView.contentSize = CGSize.init(width:CGFloat((self.dataSource?.count ?? 0)) * (itemWeight + interspace) + interspace, height: 0)
- }
-
-
- //MARK: collectionView
- lazy var scrollView : UIScrollView = {
-
- let scrollView = UIScrollView.init(frame: CGRect.zero)
- scrollView.alwaysBounceHorizontal = true
- scrollView.isScrollEnabled = true
- scrollView.showsVerticalScrollIndicator = false
- scrollView.showsHorizontalScrollIndicator = false
- scrollView.contentInsetAdjustmentBehavior = .never
- scrollView.backgroundColor = UIColor.color(hex: "#EDEDED")
- return scrollView
-
- }()
-
- //MARK: collectionView
- lazy var lineView : UIView = {
-
- let lineView = UIScrollView.init(frame: CGRect(x: 0, y: 42.5, width: 60, height: 2.5))
- lineView.backgroundColor = kThemeColor
- lineView.isHidden = true
- return lineView
-
- }()
- }
|