// // EditorDeliveryView.swift // ADHTuanCan // // Created by 敖德亨 on 2023/10/31. // import UIKit class EditorDeliveryView: UIView { @IBOutlet weak var blackView: UIView! @IBOutlet weak var viewHeigt: NSLayoutConstraint! @IBOutlet weak var collectionView: UICollectionView! var dataSource : [DeliveryTimeModel]? var selectTimeBlock : ((_ model : DeliveryTimeModel)->Void)? var chooseModle : DeliveryTimeModel? /// 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 = 0.0 layout.scrollDirection = .vertical //竖直 layout.itemSize = CGSize.init(width: (kSCREEN_WIDTH - 81 - 50)/3, height: 32) //行距 layout.minimumInteritemSpacing = 12 layout.minimumLineSpacing = 12 layout.sectionInset = .init(top: 0, left: itemSpac, bottom: 0, right: itemSpac) collectionView.collectionViewLayout = layout collectionView.backgroundColor = UIColor.clear collectionView.showsVerticalScrollIndicator = false collectionView.isScrollEnabled = false collectionView.register(withType: DeliveryTimeItem.self) } func configModel(dataSource : [DeliveryTimeModel]?){ self.dataSource = dataSource self.collectionView.reloadData() var zhengshu = (dataSource?.count ?? 0)/3 var yushu = (dataSource?.count ?? 0)%3 if yushu > 0 { yushu = 1 } self.viewHeigt.constant = 120 + 44 * CGFloat(zhengshu + yushu) - 12 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 sureAction(_ sender: UIButton) { if self.chooseModle != nil{ if (self.selectTimeBlock != nil){ self.selectTimeBlock!(self.chooseModle!) } self.hidden() }else{ self.hud.showFailure(LanguagesUtil.createTextBy(Ctext: "请选择送餐时间", Etext: "Please select a delivery time")) } } @IBAction func cancelAction(_ 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 EditorDeliveryView : UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout { func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return self.dataSource?.count ?? 0 } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell : DeliveryTimeItem = collectionView.dequeueReusableCell(withReuseIdentifier: "DeliveryTimeItem", for: indexPath) as! DeliveryTimeItem cell.configModel(model: self.dataSource![indexPath.row]) return cell; } func numberOfSections(in collectionView: UICollectionView) -> Int { return 1 } func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { let model = self.dataSource![indexPath.row] if model.status == "1"{ return } if model.quota == "0"{ return } for item in self.dataSource! { let data : DeliveryTimeModel = item data.selected = false } model.selected = true self.collectionView.reloadData() self.chooseModle = model } }