EditorDeliveryView.swift 4.4 KB

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