// // IHHotelPickerHeaderView.swift // Inhealth // // Created by weclouds on 2020/3/12. // Copyright © 2020 weclouds. All rights reserved. // import UIKit protocol IHHotelPickerHeaderViewDelegate : NSObjectProtocol{ func pickViewToSelectCountry(_ countryId:String) func pickCurrentHotel() //选择的历史酒店 func pickHistoryHotel(_ hotelId:String,hotelName:String) } class IHHotelPickerHeaderView: UIView { weak var delegate : IHHotelPickerHeaderViewDelegate? private var countryLabel :UILabel! //国家label private var hotelLabel:UILabel! var countries:[DropDownData]?//国家列表 var bottomLine:UIView? var historyarr:[IHHistoryHotelModel]? { didSet{ if let arr = self.historyarr { self.createHistoryBottons(arr) } } } private var historyBox :UIView? //历史数据内容 var currentCountry:String? //当前国家 { didSet{ self.countryLabel.text = self.currentCountry } } var currentContryId:String? var currentHotel:String?{ didSet{ self.hotelLabel.text = self.currentHotel } } override init(frame: CGRect) { super.init(frame: frame) self.backgroundColor = .red setupUI() NotificationCenter.default.addObserver(self, selector: #selector(reloadAddressNotify), name: NSNotification.Name(kNotificationIHAddressControllerReloadAddress), object: nil) } //刷新地址 @objc func reloadAddressNotify() { log.debug("刷新了") self.currentHotel = Intermediate.hotelName } deinit { NotificationCenter.default.removeObserver(self) } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } override func layoutSubviews() { super.layoutSubviews() } } extension IHHotelPickerHeaderView{ func setupUI() { //第一栏 let box1 = UIView(frame: CGRect(x: 20, y: 15, width: KSCREENWIDTH - 40, height: 57)) box1.backgroundColor = UIColor(hexString: "#F6F8F7") box1.layer.masksToBounds = true box1.layer.cornerRadius = 5 self.addSubview(box1) //添加点击手势 box1.isUserInteractionEnabled = true let tapHotel = UITapGestureRecognizer(target: self, action: #selector(tapCurrentHotel)) box1.addGestureRecognizer(tapHotel) setupBox1(box1) //第二栏 let box2 = UIView(frame: CGRect(x: 20, y: 15 + 57 + 10, width: KSCREENWIDTH - 40, height: 40)) box2.backgroundColor = UIColor(hexString: "#F6F8F7") box2.layer.masksToBounds = true box2.layer.cornerRadius = 5 self.addSubview(box2) setupBox2(box2) //给国家添加手势 box2.isUserInteractionEnabled = true let tap = UITapGestureRecognizer(target: self, action: #selector(selectedCountry)) box2.addGestureRecognizer(tap) //第三栏 let box3Y : CGFloat = 15 + 57 + 10 + 40 + 10 self.historyBox = UIView(frame: CGRect(x: 0, y: box3Y, width: KSCREENWIDTH, height: self.bounds.size.height - box3Y)) historyBox!.backgroundColor = .white self.addSubview(self.historyBox!) //底部线条 bottomLine = UIView(frame: CGRect(x: 0, y: self.bounds.size.height - 1, width: KSCREENWIDTH, height: 1)) bottomLine?.backgroundColor = UIColor(hexString: "#EBEFF2") addSubview(bottomLine!) //顶部线 let topLine = UIView(frame: CGRect(x: 0, y: 0, width: KSCREENWIDTH, height: 1)) topLine.backgroundColor = UIColor(hexString: "#EBEFF2") addSubview(topLine) setupBox3(historyBox!) } func setupBox1(_ box : UIView) { // let imageV = UIImageView(image: UIImage(named: "旅游主题_酒店")) // box.addSubview(imageV) // imageV.snp.makeConstraints { (make) in // make.left.equalToSuperview().offset(15) // make.centerY.equalToSuperview() // make.width.height.equalTo(19) // } //当前酒店 let currentLabel = UILabel() currentLabel.text = "当前位置" currentLabel.textColor = UIColor(hexString: "#333333") currentLabel.font = UIFont(name: PingFangSC_Regular, size: 13) box.addSubview(currentLabel) currentLabel.snp.makeConstraints { (make) in make.left.equalToSuperview().offset(15) make.centerY.equalToSuperview() make.height.equalTo(30) make.width.equalTo(60) } //imageV // currentLabel.snp.makeConstraints { (make) in // make.left.equalTo(currentLabel.snp.right).offset(7) // make.centerY.equalToSuperview() // } //设置选择酒店结果的label let hotelLabel = UILabel() hotelLabel.textColor = UIColor(hexString: "#333333") hotelLabel.font = UIFont(name: PingFangSC_Semibold, size: 15) hotelLabel.text = "ShenZhen hotel" hotelLabel.textAlignment = .right box.addSubview(hotelLabel) hotelLabel.snp.makeConstraints { (make) in make.right.equalToSuperview().offset(-15) make.left.equalTo(currentLabel.snp.right).offset(5) make.centerY.equalToSuperview() } self.hotelLabel = hotelLabel } func setupBox2(_ box:UIView) { let imageV = UIImageView(image: UIImage(named: "定位")) box.addSubview(imageV) imageV.snp.makeConstraints { (make) in make.left.equalToSuperview().offset(15) make.centerY.equalToSuperview() make.width.height.equalTo(16) } //当前国家 let currentLabel = UILabel() currentLabel.text = "China" currentLabel.textColor = UIColor(hexString: "#666666") currentLabel.font = UIFont(name: Alibaba_PuHuiTi_Bold, size: 14) box.addSubview(currentLabel) currentLabel.snp.makeConstraints { (make) in make.left.equalTo(imageV.snp.right).offset(4) make.centerY.equalToSuperview() } self.countryLabel = currentLabel let imageV2 = UIImageView(image: UIImage(named: "youpoint")) box.addSubview(imageV2) imageV2.snp.makeConstraints { (make) in make.right.equalToSuperview().offset(-15) make.centerY.equalToSuperview() make.width.height.equalTo(16) } } func setupBox3(_ box: UIView) { let historicalLabel = UILabel() historicalLabel.text = "历史记录" historicalLabel.textColor = UIColor(hexString: "#333333") historicalLabel.font = UIFont(name: Alibaba_PuHuiTi_Medium, size: 14) box.addSubview(historicalLabel) historicalLabel.snp.makeConstraints { (make) in make.left.equalToSuperview().offset(20) make.top.equalToSuperview().offset(5) } } func createHistoryBottons(_ titleArr:[IHHistoryHotelModel]) { let box3Y : CGFloat = 15 + 57 + 10 + 40 + 10 self.historyBox?.height = self.bounds.size.height - box3Y for j in 0.. bgView_width{ positionX = 20 positionY += 40 } let hisBtn = createHistoricalItem() hisBtn.frame = CGRect(x: positionX, y: positionY, width: btnWidth, height: lab_h) hisBtn.setTitle(str, for: .normal) positionX += (btnWidth + 10) hisBtn.tag = 3500 + i hisBtn.addTarget(self, action: #selector(historyBtnClick), for: .touchUpInside) self.historyBox!.addSubview(hisBtn) } //底部线的高度调整 self.bottomLine?.y = self.bounds.size.height - 1 } //创建历史数据Label func createHistoricalItem() ->UIButton { let btn = UIButton(type: .custom) btn.titleLabel!.font = UIFont(name: PingFangSC_Regular, size: 13) btn.setTitleColor(UIColor(hexString: "#333333"), for: .normal) btn.layer.cornerRadius = 5 btn.layer.masksToBounds = true btn.backgroundColor = UIColor(hexString: "#F6F8F7") return btn } @objc func historyBtnClick(_ sender:UIButton) { log.debug("点击了历史数据按钮") let index = sender.tag - 3500 let hotel = self.historyarr![index] if let delegate = self.delegate { // delegate.pickHistoryCity(city.id!, cityName: city.name!) delegate.pickHistoryHotel(hotel.id!, hotelName: hotel.name!) } } @objc func selectedCountry() { if countries?.count == 0 || countries == nil{ return } var titleArr = [String]() for country in self.countries! { titleArr.append(country.name ?? "") } let pick = THScrollChooseView(question: titleArr, withDefaultDesc: self.currentCountry ?? titleArr.first) pick?.confirmBlock = {(selectedIndex) in log.debug(" title = \(titleArr[selectedIndex])") self.countryLabel.text = "\(titleArr[selectedIndex])" if let delegate = self.delegate { let country = self.countries![selectedIndex] self.currentCountry = country.name Intermediate.countryId = country.id! Intermediate.countryName = country.name! //countryId: Optional("1"), countryName: Optional("Albania") delegate.pickViewToSelectCountry(country.id!) } } pick?.show() } @objc func tapCurrentHotel() { log.debug("tapCurrentHotel") if let delegate = self.delegate { delegate.pickCurrentHotel() } } }