IHFloorMapSelectedView.swift 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. //
  2. // IHFloorMapSelectedView.swift
  3. // Inhealth
  4. //
  5. // Created by weclouds on 2020/1/14.
  6. // Copyright © 2020 weclouds. All rights reserved.
  7. //
  8. import UIKit
  9. protocol IHFloorMapSelectedViewDelegate : NSObjectProtocol {
  10. func selected(build:DropDownData,floor:RoomListData)
  11. }
  12. class IHFloorMapSelectedView: UIView {
  13. weak var delegate :IHFloorMapSelectedViewDelegate?
  14. var hotelId :String? = "0"
  15. var buildId:String? = "0"
  16. var buildListData : [DropDownData]?{
  17. didSet{
  18. }
  19. }
  20. deinit {
  21. log.debug("IHFloorMapSelectedView销毁")
  22. }
  23. var floorList:[RoomListData]?
  24. var selectedBuildIndex:Int? = 0
  25. var selectedFloorIndex:Int? = 0
  26. lazy var pickerView: UIPickerView = {
  27. let pickerView = UIPickerView(frame: CGRect(x: 0, y: 45, width: KSCREENWIDTH, height: 180))
  28. pickerView.delegate = self
  29. pickerView.dataSource = self
  30. return pickerView
  31. }()
  32. var bottomView:UIView?
  33. var cancelButton :UIButton?
  34. var confirmButton :UIButton?
  35. convenience init(buildListData:[DropDownData]) {
  36. self.init()
  37. if buildListData .count > 0{
  38. self.buildListData = buildListData
  39. self.pickerView.reloadComponent(0)
  40. if let datalist = self.buildListData {
  41. //默认后去第一个楼房的楼层
  42. let firstBuild = datalist[0]
  43. self.getFloorList(firstBuild.id!)
  44. self.buildId = firstBuild.id
  45. }
  46. }
  47. }
  48. override init(frame: CGRect) {
  49. super.init(frame: frame)
  50. createUI()
  51. }
  52. required init?(coder: NSCoder) {
  53. fatalError("init(coder:) has not been implemented")
  54. }
  55. func show() {
  56. self.pickerView.selectRow(0, inComponent: 0, animated: false)
  57. selectedBuildIndex = 0
  58. UIApplication.shared.keyWindow?.addSubview(self)
  59. self.alpha = 0
  60. UIView.animate(withDuration: 0.3) {
  61. self.alpha = 1
  62. }
  63. }
  64. func dismiss() {
  65. UIView.animate(withDuration: 0.3, animations: {
  66. self.alpha = 0
  67. }) { (finished) in
  68. self.removeFromSuperview()
  69. }
  70. }
  71. override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
  72. dismiss()
  73. }
  74. }
  75. extension IHFloorMapSelectedView{
  76. func createUI() {
  77. self.backgroundColor = UIColor.black.withAlphaComponent(0.3)
  78. self.frame = CGRect(x: 0, y: 0, width: KSCREENWIDTH, height: KSCREENHEIGHT)
  79. let whiteView = UIView(frame: CGRect(x: 0, y: KSCREENHEIGHT - 260, width: KSCREENWIDTH, height: 260))
  80. whiteView.backgroundColor = UIColor.white
  81. addSubview(whiteView)
  82. //按钮所在区域
  83. let viewBg = UIView(frame: CGRect(x: 0, y: 0, width: KSCREENWIDTH, height: 45 ))
  84. whiteView.addSubview(viewBg)
  85. //创建取消 确定按钮
  86. let cancelButton = UIButton(type: .custom)
  87. cancelButton.frame = CGRect(x: 20, y: 0, width: 50, height: 45)
  88. cancelButton.setTitle("取消", for: .normal)
  89. cancelButton.titleLabel?.font = UIFont(name: PingFangSC_Semibold, size: 13)
  90. cancelButton.setTitleColor(UIColor.darkGray, for: .normal)
  91. cancelButton.addTarget(self, action: #selector(cancelButtonAction), for: .touchUpInside)
  92. viewBg.addSubview(cancelButton)
  93. let confirmButton = UIButton(type: .custom)
  94. confirmButton.frame = CGRect(x:KSCREENWIDTH - 70, y: 0, width: 50, height: 45)
  95. confirmButton.setTitle("确定", for: .normal)
  96. confirmButton.titleLabel?.font = UIFont(name: PingFangSC_Semibold, size: 13)
  97. confirmButton.setTitleColor(UIColor.darkGray, for: .normal)
  98. confirmButton.addTarget(self, action: #selector(confirmButtonAction), for: .touchUpInside)
  99. viewBg.addSubview(confirmButton)
  100. whiteView.addSubview(self.pickerView)
  101. }
  102. @objc func cancelButtonAction() {
  103. dismiss()
  104. }
  105. @objc func confirmButtonAction() {
  106. if let delegate = self.delegate {
  107. let build = self.buildListData![selectedBuildIndex!]
  108. let floor = self.floorList![selectedFloorIndex!]
  109. delegate.selected(build: build, floor: floor)
  110. }
  111. dismiss()
  112. }
  113. func requestData() {
  114. IHAreaService.share.getBuildNavData(hotelId: nil, requestSuccess: { (dataList) in
  115. self.buildListData = dataList
  116. }) {
  117. }
  118. }
  119. func getFloorList(_ buildId:String) {
  120. IHAreaService.share.getRoomListData(buildId: buildId, keyworkds: nil, requestSuccess: { (roomlist) in
  121. self.floorList = roomlist
  122. self.pickerView.reloadComponent(1)
  123. self.pickerView.selectRow(0, inComponent: 1, animated: true)
  124. self.selectedFloorIndex = 0
  125. }) {
  126. }
  127. }
  128. }
  129. extension IHFloorMapSelectedView: UIPickerViewDataSource,UIPickerViewDelegate{
  130. func numberOfComponents(in pickerView: UIPickerView) -> Int {
  131. return 2
  132. }
  133. func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
  134. if component == 0 {
  135. return self.buildListData?.count ?? 0
  136. }else if component == 1{
  137. return self.floorList?.count ?? 0
  138. }
  139. return 0
  140. }
  141. func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
  142. return 30
  143. }
  144. func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
  145. if component == 0 {
  146. let buildLabel = UILabel(frame: CGRect(x: 0, y: 0, width: KSCREENWIDTH / 2, height: 30))
  147. buildLabel.textColor = UIColor(hexString: "222222")
  148. buildLabel.font = UIFont(name: PingFangSC_Regular, size: 15)
  149. buildLabel.textAlignment = .center
  150. let model = self.buildListData![row]
  151. buildLabel.text = model.name
  152. return buildLabel
  153. }else if component == 1{
  154. let floorLabel = UILabel(frame: CGRect(x: KSCREENWIDTH / 2, y: 0, width: KSCREENWIDTH / 2, height: 30))
  155. floorLabel.textColor = UIColor(hexString: "222222")
  156. floorLabel.font = UIFont(name: PingFangSC_Regular, size: 15)
  157. floorLabel.textAlignment = .center
  158. let model = self.floorList![row]
  159. floorLabel.text = model.name
  160. return floorLabel
  161. }
  162. return UIView()
  163. }
  164. func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
  165. if component == 0 {
  166. selectedBuildIndex = row
  167. let build = self.buildListData![row]
  168. getFloorList(build.id!)
  169. } else {
  170. selectedFloorIndex = row
  171. }
  172. }
  173. }