// // ReplaceFoodView.swift // ADHTuanCan // // Created by 敖德亨 on 2023/10/30. // import UIKit class ReplaceFoodView: UIView { @IBOutlet weak var blackView: UIView! @IBOutlet weak var titleLab: UILabel! @IBOutlet weak var collectView: UICollectionView! @IBOutlet weak var collectionViewH: NSLayoutConstraint! var dataModel : [MealFoodMsgModel]? var row : Int? var selectModel : MealFoodMsgModel? var selectBlock : ((_ model : MealFoodMsgModel ,_ row : Int)->Void)? /// hud 提示 lazy var hud : MCHud! = { return MCHud() }() override func awakeFromNib() { super.awakeFromNib() self.createCollectView() } //MARK: collectionView func createCollectView(){ //设置布局 let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout.init() let itemSpac = 10.0 layout.scrollDirection = .vertical //竖直 layout.itemSize = CGSize.init(width: ((kSCREEN_WIDTH - 50) - 40)/3, height: 116) //行距 layout.minimumInteritemSpacing = itemSpac layout.minimumLineSpacing = itemSpac layout.sectionInset = .init(top: itemSpac, left: itemSpac, bottom: 0, right: itemSpac) collectView.collectionViewLayout = layout collectView.backgroundColor = UIColor.clear collectView.delegate = self collectView.dataSource = self collectView.showsVerticalScrollIndicator = false collectView.isScrollEnabled = true collectView.register(withType: SetMealCollectionItem.self) collectionViewH.constant = CGFloat(128 * 3) } /// 展示 public func showWithModel(models : [MealFoodMsgModel]! , row : Int ,foodName : String){ self.titleLab.text = LanguagesUtil.createTextBy(Ctext: "对\(foodName ?? "")进行替换", Etext: "Replace \(foodName ?? "")\(foodName ?? "")\(foodName ?? "")\(foodName ?? "")") self.dataModel = models self.row = row self.collectView.reloadData() kAppDelegateWindow.addSubview(self) self.blackView.alpha = 0 self.frame = CGRect.init(x: 0, y: kSCREEN_HEIGHT, width: kSCREEN_WIDTH, height: kSCREEN_HEIGHT) UIView.animate(withDuration: 0.5) { self.frame = CGRect.init(x: 0, y: 0, width: kSCREEN_WIDTH, height: kSCREEN_HEIGHT) self.layoutIfNeeded() } DELAY(0.5) { self.blackView.alpha = 0.3 } } @IBAction func makeSureAction(_ sender: UIButton) { if self.selectModel != nil{ if (self.selectBlock != nil){ self.selectBlock!(self.selectModel!,self.row!) } self.hidden() }else{ self.hud.showFailure(LanguagesUtil.createTextBy(Ctext: "请选择替换菜品", Etext: "Please select alternative dishes")) } } @IBAction func cancelActiom(_ sender: UIButton) { self.hidden() } /// 隐藏 public func hidden(){ self.blackView.alpha = 0 UIView.animate(withDuration: 0.5) { self.frame = CGRect.init(x: 0, y: kSCREEN_HEIGHT, width: kSCREEN_WIDTH, height: kSCREEN_HEIGHT) self.layoutIfNeeded() } DELAY(0.5) { self.removeFromSuperview() } } } //MARK: Delegate extension ReplaceFoodView : UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout { func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return self.dataModel?.count ?? 0 } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell : SetMealCollectionItem = collectionView.dequeueReusableCell(withReuseIdentifier: "SetMealCollectionItem", for: indexPath) as! SetMealCollectionItem cell.configData(model: self.dataModel![indexPath.row]) return cell; } func numberOfSections(in collectionView: UICollectionView) -> Int { return 1 } func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { let model = self.dataModel![indexPath.row] if model.selected { return } for item in self.dataModel! { let data : MealFoodMsgModel = item data.selected = false } model.selected = true self.selectModel = model self.collectView.reloadData() } }