123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- //
- // HomeRecommendView.swift
- // ADHTuanCan
- //
- // Created by 敖德亨 on 2023/9/30.
- //
- import UIKit
- class HomeRecommendView: UIView {
- private var scrollView : UIScrollView!
-
- var dataModel : SupplyTypeListModel?
-
- var allBtns : [UIButton] = []
-
- var runTypeAction : ((String?)->Void)?
-
- var btnW = (kSCREEN_WIDTH - 72)/3
-
- override func awakeFromNib() {
- super.awakeFromNib()
- self.backgroundColor = kTBackgroundColor
- self.scrollView = UIScrollView.init(frame: CGRect.zero)
- self.scrollView.alwaysBounceHorizontal = true
- self.scrollView.isScrollEnabled = true
- self.scrollView.showsHorizontalScrollIndicator = false
- self.scrollView.contentInsetAdjustmentBehavior = .never
- self.scrollView.backgroundColor = UIColor.white
-
- self.addSubview(self.scrollView)
-
- self.scrollView.mas_remakeConstraints { make in
- make?.left.right().top().bottom().mas_equalTo()(self)
- make?.height.offset()(36 + 40)
- }
- }
-
- func configTitle(model : SupplyTypeListModel){
-
- dataModel = model
- var views = scrollView.subviews
- views.removeAll()
- self.allBtns.removeAll()
- if let _ = model.runTypeList{
- for i in 0..<model.runTypeList!.count{
-
- let detail : RunTypeModel = model.runTypeList![i]
-
- let btn:UIButton = UIButton.init(type: UIButton.ButtonType.custom);//新建btn
- btn.frame = CGRect.init(x: 24 + (btnW + 24) * CGFloat(i), y: 20, width: btnW, height: 36)
- btn.selectedBackgroundColor = kThemeColor
- btn.layer.cornerRadius = 6
- btn.layer.masksToBounds = true
- btn.setTitle("\(detail.name ?? "")", for: UIControl.State.normal)
- btn.setBorderWidth("1", forState: .normal)
- btn.setTitleColor(UIColor.color(hex: "#1C304F"), for: .normal)
- btn.setTitleColor(UIColor.white, for: .selected)
- btn.backgroundColor = UIColor.white
- btn.setBorderColor(UIColor.color(hex: "#EEEEEE"), forState: .normal)
- btn.setBorderColor(kThemeColor, forState: .selected)
-
- btn.isSelected = false
- btn.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: .medium)
- btn.tag = i
- btn.addTarget(self, action: #selector(btnSelectAction(sender: )), for: .touchUpInside)
- if i == 0{
- btn.isSelected = true
- btn.backgroundColor = kThemeColor
- }
- self.scrollView.addSubview(btn)
- allBtns.append(btn)
- }
- self.createLayer()
- }
- }
-
- func createLayer(){
-
- scrollView.contentSize = CGSize.init(width: CGFloat(dataModel!.runTypeList!.count) * (24 + btnW) + 24, height: 0)
-
- }
-
- @objc private func btnSelectAction(sender : UIButton){
-
- if sender.isSelected == true{
- return
- }
- for item in self.allBtns {
- let btn : UIButton = item
- btn.isSelected = false
- btn.backgroundColor = UIColor.white
- }
- sender.isSelected = true
- sender.backgroundColor = kThemeColor
-
- let detail = dataModel!.runTypeList![sender.tag]
- if (self.runTypeAction != nil)
- {
- self.runTypeAction!(detail.id)
- }
- }
- }
|