IHHotelPickerHeaderView.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. //
  2. // IHHotelPickerHeaderView.swift
  3. // Inhealth
  4. //
  5. // Created by weclouds on 2020/3/12.
  6. // Copyright © 2020 weclouds. All rights reserved.
  7. //
  8. import UIKit
  9. protocol IHHotelPickerHeaderViewDelegate : NSObjectProtocol{
  10. func pickViewToSelectCountry(_ countryId:String)
  11. func pickCurrentHotel()
  12. //选择的历史酒店
  13. func pickHistoryHotel(_ hotelId:String,hotelName:String)
  14. }
  15. class IHHotelPickerHeaderView: UIView {
  16. weak var delegate : IHHotelPickerHeaderViewDelegate?
  17. private var countryLabel :UILabel! //国家label
  18. private var hotelLabel:UILabel!
  19. var countries:[DropDownData]?//国家列表
  20. var bottomLine:UIView?
  21. var historyarr:[IHHistoryHotelModel]? {
  22. didSet{
  23. if let arr = self.historyarr {
  24. self.createHistoryBottons(arr)
  25. }
  26. }
  27. }
  28. private var historyBox :UIView? //历史数据内容
  29. var currentCountry:String? //当前国家
  30. {
  31. didSet{
  32. self.countryLabel.text = self.currentCountry
  33. }
  34. }
  35. var currentContryId:String?
  36. var currentHotel:String?{
  37. didSet{
  38. self.hotelLabel.text = self.currentHotel
  39. }
  40. }
  41. override init(frame: CGRect) {
  42. super.init(frame: frame)
  43. self.backgroundColor = .red
  44. setupUI()
  45. NotificationCenter.default.addObserver(self, selector: #selector(reloadAddressNotify), name: NSNotification.Name(kNotificationIHAddressControllerReloadAddress), object: nil)
  46. }
  47. //刷新地址
  48. @objc func reloadAddressNotify() {
  49. log.debug("刷新了")
  50. self.currentHotel = Intermediate.hotelName
  51. }
  52. deinit {
  53. NotificationCenter.default.removeObserver(self)
  54. }
  55. required init?(coder: NSCoder) {
  56. fatalError("init(coder:) has not been implemented")
  57. }
  58. override func layoutSubviews() {
  59. super.layoutSubviews()
  60. }
  61. }
  62. extension IHHotelPickerHeaderView{
  63. func setupUI() {
  64. //第一栏
  65. let box1 = UIView(frame: CGRect(x: 20, y: 15, width: KSCREENWIDTH - 40, height: 57))
  66. box1.backgroundColor = UIColor(hexString: "#F6F8F7")
  67. box1.layer.masksToBounds = true
  68. box1.layer.cornerRadius = 5
  69. self.addSubview(box1)
  70. //添加点击手势
  71. box1.isUserInteractionEnabled = true
  72. let tapHotel = UITapGestureRecognizer(target: self, action: #selector(tapCurrentHotel))
  73. box1.addGestureRecognizer(tapHotel)
  74. setupBox1(box1)
  75. //第二栏
  76. let box2 = UIView(frame: CGRect(x: 20, y: 15 + 57 + 10, width: KSCREENWIDTH - 40, height: 40))
  77. box2.backgroundColor = UIColor(hexString: "#F6F8F7")
  78. box2.layer.masksToBounds = true
  79. box2.layer.cornerRadius = 5
  80. self.addSubview(box2)
  81. setupBox2(box2)
  82. //给国家添加手势
  83. box2.isUserInteractionEnabled = true
  84. let tap = UITapGestureRecognizer(target: self, action: #selector(selectedCountry))
  85. box2.addGestureRecognizer(tap)
  86. //第三栏
  87. let box3Y : CGFloat = 15 + 57 + 10 + 40 + 10
  88. self.historyBox = UIView(frame: CGRect(x: 0, y: box3Y, width: KSCREENWIDTH, height: self.bounds.size.height - box3Y))
  89. historyBox!.backgroundColor = .white
  90. self.addSubview(self.historyBox!)
  91. //底部线条
  92. bottomLine = UIView(frame: CGRect(x: 0, y: self.bounds.size.height - 1, width: KSCREENWIDTH, height: 1))
  93. bottomLine?.backgroundColor = UIColor(hexString: "#EBEFF2")
  94. addSubview(bottomLine!)
  95. //顶部线
  96. let topLine = UIView(frame: CGRect(x: 0, y: 0, width: KSCREENWIDTH, height: 1))
  97. topLine.backgroundColor = UIColor(hexString: "#EBEFF2")
  98. addSubview(topLine)
  99. setupBox3(historyBox!)
  100. }
  101. func setupBox1(_ box : UIView) {
  102. // let imageV = UIImageView(image: UIImage(named: "旅游主题_酒店"))
  103. // box.addSubview(imageV)
  104. // imageV.snp.makeConstraints { (make) in
  105. // make.left.equalToSuperview().offset(15)
  106. // make.centerY.equalToSuperview()
  107. // make.width.height.equalTo(19)
  108. // }
  109. //当前酒店
  110. let currentLabel = UILabel()
  111. currentLabel.text = "当前位置"
  112. currentLabel.textColor = UIColor(hexString: "#333333")
  113. currentLabel.font = UIFont(name: PingFangSC_Regular, size: 13)
  114. box.addSubview(currentLabel)
  115. currentLabel.snp.makeConstraints { (make) in
  116. make.left.equalToSuperview().offset(15)
  117. make.centerY.equalToSuperview()
  118. make.height.equalTo(30)
  119. make.width.equalTo(60)
  120. }
  121. //imageV
  122. // currentLabel.snp.makeConstraints { (make) in
  123. // make.left.equalTo(currentLabel.snp.right).offset(7)
  124. // make.centerY.equalToSuperview()
  125. // }
  126. //设置选择酒店结果的label
  127. let hotelLabel = UILabel()
  128. hotelLabel.textColor = UIColor(hexString: "#333333")
  129. hotelLabel.font = UIFont(name: PingFangSC_Semibold, size: 15)
  130. hotelLabel.text = "ShenZhen hotel"
  131. hotelLabel.textAlignment = .right
  132. box.addSubview(hotelLabel)
  133. hotelLabel.snp.makeConstraints { (make) in
  134. make.right.equalToSuperview().offset(-15)
  135. make.left.equalTo(currentLabel.snp.right).offset(5)
  136. make.centerY.equalToSuperview()
  137. }
  138. self.hotelLabel = hotelLabel
  139. }
  140. func setupBox2(_ box:UIView) {
  141. let imageV = UIImageView(image: UIImage(named: "定位"))
  142. box.addSubview(imageV)
  143. imageV.snp.makeConstraints { (make) in
  144. make.left.equalToSuperview().offset(15)
  145. make.centerY.equalToSuperview()
  146. make.width.height.equalTo(16)
  147. }
  148. //当前国家
  149. let currentLabel = UILabel()
  150. currentLabel.text = "China"
  151. currentLabel.textColor = UIColor(hexString: "#666666")
  152. currentLabel.font = UIFont(name: Alibaba_PuHuiTi_Bold, size: 14)
  153. box.addSubview(currentLabel)
  154. currentLabel.snp.makeConstraints { (make) in
  155. make.left.equalTo(imageV.snp.right).offset(4)
  156. make.centerY.equalToSuperview()
  157. }
  158. self.countryLabel = currentLabel
  159. let imageV2 = UIImageView(image: UIImage(named: "youpoint"))
  160. box.addSubview(imageV2)
  161. imageV2.snp.makeConstraints { (make) in
  162. make.right.equalToSuperview().offset(-15)
  163. make.centerY.equalToSuperview()
  164. make.width.height.equalTo(16)
  165. }
  166. }
  167. func setupBox3(_ box: UIView) {
  168. let historicalLabel = UILabel()
  169. historicalLabel.text = "历史记录"
  170. historicalLabel.textColor = UIColor(hexString: "#333333")
  171. historicalLabel.font = UIFont(name: Alibaba_PuHuiTi_Medium, size: 14)
  172. box.addSubview(historicalLabel)
  173. historicalLabel.snp.makeConstraints { (make) in
  174. make.left.equalToSuperview().offset(20)
  175. make.top.equalToSuperview().offset(5)
  176. }
  177. }
  178. func createHistoryBottons(_ titleArr:[IHHistoryHotelModel]) {
  179. let box3Y : CGFloat = 15 + 57 + 10 + 40 + 10
  180. self.historyBox?.height = self.bounds.size.height - box3Y
  181. for j in 0..<titleArr.count {
  182. let tag = 3500 + j
  183. let button = self.historyBox?.viewWithTag(tag) as? UIButton
  184. if button != nil {
  185. button?.removeFromSuperview()
  186. }
  187. }
  188. let historyarr = titleArr
  189. var positionY : CGFloat = 5 + 19 + 15
  190. var positionX : CGFloat = 20
  191. let lab_h : CGFloat = 30
  192. let bgView_width : CGFloat = KSCREENWIDTH - 40
  193. for i in 0..<historyarr.count {
  194. let city = historyarr[i]
  195. let str = city.name
  196. let str_width = str!.ga_widthForComment(font: UIFont(name: PingFangSC_Regular, size: 13)!, height: 30)
  197. let btnWidth = str_width + 20
  198. if positionX + btnWidth > bgView_width{
  199. positionX = 20
  200. positionY += 40
  201. }
  202. let hisBtn = createHistoricalItem()
  203. hisBtn.frame = CGRect(x: positionX, y: positionY, width: btnWidth, height: lab_h)
  204. hisBtn.setTitle(str, for: .normal)
  205. positionX += (btnWidth + 10)
  206. hisBtn.tag = 3500 + i
  207. hisBtn.addTarget(self, action: #selector(historyBtnClick), for: .touchUpInside)
  208. self.historyBox!.addSubview(hisBtn)
  209. }
  210. //底部线的高度调整
  211. self.bottomLine?.y = self.bounds.size.height - 1
  212. }
  213. //创建历史数据Label
  214. func createHistoricalItem() ->UIButton {
  215. let btn = UIButton(type: .custom)
  216. btn.titleLabel!.font = UIFont(name: PingFangSC_Regular, size: 13)
  217. btn.setTitleColor(UIColor(hexString: "#333333"), for: .normal)
  218. btn.layer.cornerRadius = 5
  219. btn.layer.masksToBounds = true
  220. btn.backgroundColor = UIColor(hexString: "#F6F8F7")
  221. return btn
  222. }
  223. @objc func historyBtnClick(_ sender:UIButton) {
  224. log.debug("点击了历史数据按钮")
  225. let index = sender.tag - 3500
  226. let hotel = self.historyarr![index]
  227. if let delegate = self.delegate {
  228. // delegate.pickHistoryCity(city.id!, cityName: city.name!)
  229. delegate.pickHistoryHotel(hotel.id!, hotelName: hotel.name!)
  230. }
  231. }
  232. @objc func selectedCountry() {
  233. if countries?.count == 0 || countries == nil{
  234. return
  235. }
  236. var titleArr = [String]()
  237. for country in self.countries! {
  238. titleArr.append(country.name ?? "")
  239. }
  240. let pick = THScrollChooseView(question: titleArr, withDefaultDesc: self.currentCountry ?? titleArr.first)
  241. pick?.confirmBlock = {(selectedIndex) in
  242. log.debug(" title = \(titleArr[selectedIndex])")
  243. self.countryLabel.text = "\(titleArr[selectedIndex])"
  244. if let delegate = self.delegate {
  245. let country = self.countries![selectedIndex]
  246. self.currentCountry = country.name
  247. Intermediate.countryId = country.id!
  248. Intermediate.countryName = country.name!
  249. //countryId: Optional("1"), countryName: Optional("Albania")
  250. delegate.pickViewToSelectCountry(country.id!)
  251. }
  252. }
  253. pick?.show()
  254. }
  255. @objc func tapCurrentHotel() {
  256. log.debug("tapCurrentHotel")
  257. if let delegate = self.delegate {
  258. delegate.pickCurrentHotel()
  259. }
  260. }
  261. }