ReplaceFoodView.swift 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. //
  2. // ReplaceFoodView.swift
  3. // ADHTuanCan
  4. //
  5. // Created by 敖德亨 on 2023/10/30.
  6. //
  7. import UIKit
  8. class ReplaceFoodView: UIView {
  9. @IBOutlet weak var blackView: UIView!
  10. @IBOutlet weak var titleLab: UILabel!
  11. @IBOutlet weak var collectView: UICollectionView!
  12. @IBOutlet weak var collectionViewH: NSLayoutConstraint!
  13. var dataModel : [MealFoodMsgModel]?
  14. var row : Int?
  15. var selectModel : MealFoodMsgModel?
  16. var selectBlock : ((_ model : MealFoodMsgModel ,_ row : Int)->Void)?
  17. /// hud 提示
  18. lazy var hud : MCHud! = {
  19. return MCHud()
  20. }()
  21. override func awakeFromNib() {
  22. super.awakeFromNib()
  23. self.createCollectView()
  24. }
  25. //MARK: collectionView
  26. func createCollectView(){
  27. //设置布局
  28. let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout.init()
  29. let itemSpac = 10.0
  30. layout.scrollDirection = .vertical //竖直
  31. layout.itemSize = CGSize.init(width: ((kSCREEN_WIDTH - 50) - 40)/3, height: 116)
  32. //行距
  33. layout.minimumInteritemSpacing = itemSpac
  34. layout.minimumLineSpacing = itemSpac
  35. layout.sectionInset = .init(top: itemSpac, left: itemSpac, bottom: 0, right: itemSpac)
  36. collectView.collectionViewLayout = layout
  37. collectView.backgroundColor = UIColor.clear
  38. collectView.delegate = self
  39. collectView.dataSource = self
  40. collectView.showsVerticalScrollIndicator = false
  41. collectView.isScrollEnabled = true
  42. collectView.register(withType: SetMealCollectionItem.self)
  43. collectionViewH.constant = CGFloat(128 * 3)
  44. }
  45. /// 展示
  46. public func showWithModel(models : [MealFoodMsgModel]! , row : Int ,foodName : String){
  47. self.titleLab.text = LanguagesUtil.createTextBy(Ctext: "对\(foodName ?? "")进行替换", Etext: "Replace \(foodName ?? "")\(foodName ?? "")\(foodName ?? "")\(foodName ?? "")")
  48. self.dataModel = models
  49. self.row = row
  50. self.collectView.reloadData()
  51. kAppDelegateWindow.addSubview(self)
  52. self.blackView.alpha = 0
  53. self.frame = CGRect.init(x: 0, y: kSCREEN_HEIGHT, width: kSCREEN_WIDTH, height: kSCREEN_HEIGHT)
  54. UIView.animate(withDuration: 0.5) {
  55. self.frame = CGRect.init(x: 0, y: 0, width: kSCREEN_WIDTH, height: kSCREEN_HEIGHT)
  56. self.layoutIfNeeded()
  57. }
  58. DELAY(0.5) {
  59. self.blackView.alpha = 0.3
  60. }
  61. }
  62. @IBAction func makeSureAction(_ sender: UIButton) {
  63. if self.selectModel != nil{
  64. if (self.selectBlock != nil){
  65. self.selectBlock!(self.selectModel!,self.row!)
  66. }
  67. self.hidden()
  68. }else{
  69. self.hud.showFailure(LanguagesUtil.createTextBy(Ctext: "请选择替换菜品", Etext: "Please select alternative dishes"))
  70. }
  71. }
  72. @IBAction func cancelActiom(_ sender: UIButton) {
  73. self.hidden()
  74. }
  75. /// 隐藏
  76. public func hidden(){
  77. self.blackView.alpha = 0
  78. UIView.animate(withDuration: 0.5) {
  79. self.frame = CGRect.init(x: 0, y: kSCREEN_HEIGHT, width: kSCREEN_WIDTH, height: kSCREEN_HEIGHT)
  80. self.layoutIfNeeded()
  81. }
  82. DELAY(0.5) {
  83. self.removeFromSuperview()
  84. }
  85. }
  86. }
  87. //MARK: Delegate
  88. extension ReplaceFoodView : UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout
  89. {
  90. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  91. return self.dataModel?.count ?? 0
  92. }
  93. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  94. let cell : SetMealCollectionItem = collectionView.dequeueReusableCell(withReuseIdentifier: "SetMealCollectionItem", for: indexPath) as! SetMealCollectionItem
  95. cell.configData(model: self.dataModel![indexPath.row])
  96. return cell;
  97. }
  98. func numberOfSections(in collectionView: UICollectionView) -> Int {
  99. return 1
  100. }
  101. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  102. let model = self.dataModel![indexPath.row]
  103. if model.selected {
  104. return
  105. }
  106. for item in self.dataModel! {
  107. let data : MealFoodMsgModel = item
  108. data.selected = false
  109. }
  110. model.selected = true
  111. self.selectModel = model
  112. self.collectView.reloadData()
  113. }
  114. }