Commit 071393e3 authored by pete谢兆麟's avatar pete谢兆麟

地址选择器

parent ed76d8c5
...@@ -51,6 +51,12 @@ ...@@ -51,6 +51,12 @@
0468D4512B67721700CFB916 /* YHMainInformationCardTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0468D4502B67721700CFB916 /* YHMainInformationCardTableViewCell.swift */; }; 0468D4512B67721700CFB916 /* YHMainInformationCardTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0468D4502B67721700CFB916 /* YHMainInformationCardTableViewCell.swift */; };
0468D4532B6782BE00CFB916 /* YHCardExampleView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0468D4522B6782BE00CFB916 /* YHCardExampleView.swift */; }; 0468D4532B6782BE00CFB916 /* YHCardExampleView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0468D4522B6782BE00CFB916 /* YHCardExampleView.swift */; };
0468D4552B6889A500CFB916 /* YHDatePickView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0468D4542B6889A500CFB916 /* YHDatePickView.swift */; }; 0468D4552B6889A500CFB916 /* YHDatePickView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0468D4542B6889A500CFB916 /* YHDatePickView.swift */; };
0468D4772B68A08600CFB916 /* area.plist in Resources */ = {isa = PBXBuildFile; fileRef = 0468D4712B68A08600CFB916 /* area.plist */; };
0468D4782B68A08600CFB916 /* YHAddressModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0468D4722B68A08600CFB916 /* YHAddressModel.swift */; };
0468D4792B68A08600CFB916 /* YHAddressViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0468D4732B68A08600CFB916 /* YHAddressViewController.swift */; };
0468D47A2B68A08600CFB916 /* YHAddressPickViewTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0468D4742B68A08600CFB916 /* YHAddressPickViewTableViewCell.swift */; };
0468D47B2B68A08600CFB916 /* YHAddressPresentAnimated.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0468D4752B68A08600CFB916 /* YHAddressPresentAnimated.swift */; };
0468D47C2B68A08600CFB916 /* YHAddressPickView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0468D4762B68A08600CFB916 /* YHAddressPickView.swift */; };
04808C062B4686510056D53C /* ATAuthSDK.bundle in Resources */ = {isa = PBXBuildFile; fileRef = 04808C042B4686510056D53C /* ATAuthSDK.bundle */; }; 04808C062B4686510056D53C /* ATAuthSDK.bundle in Resources */ = {isa = PBXBuildFile; fileRef = 04808C042B4686510056D53C /* ATAuthSDK.bundle */; };
04808C082B4686C10056D53C /* ATAuthSDK_D.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 04808C032B4686510056D53C /* ATAuthSDK_D.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; }; 04808C082B4686C10056D53C /* ATAuthSDK_D.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 04808C032B4686510056D53C /* ATAuthSDK_D.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
049A48A82B49417300D0C641 /* YHAboutUsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 049A48A72B49417300D0C641 /* YHAboutUsViewController.swift */; }; 049A48A82B49417300D0C641 /* YHAboutUsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 049A48A72B49417300D0C641 /* YHAboutUsViewController.swift */; };
...@@ -236,6 +242,12 @@ ...@@ -236,6 +242,12 @@
0468D4502B67721700CFB916 /* YHMainInformationCardTableViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = YHMainInformationCardTableViewCell.swift; sourceTree = "<group>"; }; 0468D4502B67721700CFB916 /* YHMainInformationCardTableViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = YHMainInformationCardTableViewCell.swift; sourceTree = "<group>"; };
0468D4522B6782BE00CFB916 /* YHCardExampleView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = YHCardExampleView.swift; sourceTree = "<group>"; }; 0468D4522B6782BE00CFB916 /* YHCardExampleView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = YHCardExampleView.swift; sourceTree = "<group>"; };
0468D4542B6889A500CFB916 /* YHDatePickView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = YHDatePickView.swift; sourceTree = "<group>"; }; 0468D4542B6889A500CFB916 /* YHDatePickView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = YHDatePickView.swift; sourceTree = "<group>"; };
0468D4712B68A08600CFB916 /* area.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = area.plist; sourceTree = "<group>"; };
0468D4722B68A08600CFB916 /* YHAddressModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = YHAddressModel.swift; sourceTree = "<group>"; };
0468D4732B68A08600CFB916 /* YHAddressViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = YHAddressViewController.swift; sourceTree = "<group>"; };
0468D4742B68A08600CFB916 /* YHAddressPickViewTableViewCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = YHAddressPickViewTableViewCell.swift; sourceTree = "<group>"; };
0468D4752B68A08600CFB916 /* YHAddressPresentAnimated.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = YHAddressPresentAnimated.swift; sourceTree = "<group>"; };
0468D4762B68A08600CFB916 /* YHAddressPickView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = YHAddressPickView.swift; sourceTree = "<group>"; };
04808C032B4686510056D53C /* ATAuthSDK_D.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = ATAuthSDK_D.framework; sourceTree = "<group>"; }; 04808C032B4686510056D53C /* ATAuthSDK_D.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = ATAuthSDK_D.framework; sourceTree = "<group>"; };
04808C042B4686510056D53C /* ATAuthSDK.bundle */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.plug-in"; path = ATAuthSDK.bundle; sourceTree = "<group>"; }; 04808C042B4686510056D53C /* ATAuthSDK.bundle */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.plug-in"; path = ATAuthSDK.bundle; sourceTree = "<group>"; };
049A48A72B49417300D0C641 /* YHAboutUsViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = YHAboutUsViewController.swift; sourceTree = "<group>"; }; 049A48A72B49417300D0C641 /* YHAboutUsViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = YHAboutUsViewController.swift; sourceTree = "<group>"; };
...@@ -511,6 +523,7 @@ ...@@ -511,6 +523,7 @@
0468D4402B61F6C400CFB916 /* V */ = { 0468D4402B61F6C400CFB916 /* V */ = {
isa = PBXGroup; isa = PBXGroup;
children = ( children = (
0468D4702B68A08600CFB916 /* YHAddressPicker */,
0468D4442B62064700CFB916 /* YHStepView.swift */, 0468D4442B62064700CFB916 /* YHStepView.swift */,
0468D4462B624AF000CFB916 /* YHBottomNextView.swift */, 0468D4462B624AF000CFB916 /* YHBottomNextView.swift */,
0468D4482B6258A900CFB916 /* YHItemView.swift */, 0468D4482B6258A900CFB916 /* YHItemView.swift */,
...@@ -530,6 +543,19 @@ ...@@ -530,6 +543,19 @@
path = VM; path = VM;
sourceTree = "<group>"; sourceTree = "<group>";
}; };
0468D4702B68A08600CFB916 /* YHAddressPicker */ = {
isa = PBXGroup;
children = (
0468D4712B68A08600CFB916 /* area.plist */,
0468D4722B68A08600CFB916 /* YHAddressModel.swift */,
0468D4732B68A08600CFB916 /* YHAddressViewController.swift */,
0468D4742B68A08600CFB916 /* YHAddressPickViewTableViewCell.swift */,
0468D4752B68A08600CFB916 /* YHAddressPresentAnimated.swift */,
0468D4762B68A08600CFB916 /* YHAddressPickView.swift */,
);
path = YHAddressPicker;
sourceTree = "<group>";
};
04808C022B4686510056D53C /* Frameworks */ = { 04808C022B4686510056D53C /* Frameworks */ = {
isa = PBXGroup; isa = PBXGroup;
children = ( children = (
...@@ -1106,6 +1132,7 @@ ...@@ -1106,6 +1132,7 @@
04808C062B4686510056D53C /* ATAuthSDK.bundle in Resources */, 04808C062B4686510056D53C /* ATAuthSDK.bundle in Resources */,
A5C5B2EE2B4F9B8800A7C5D1 /* Localizable.xcstrings in Resources */, A5C5B2EE2B4F9B8800A7C5D1 /* Localizable.xcstrings in Resources */,
A58951C52B398D1000225C19 /* Launch Screen.storyboard in Resources */, A58951C52B398D1000225C19 /* Launch Screen.storyboard in Resources */,
0468D4772B68A08600CFB916 /* area.plist in Resources */,
A51044182B493675006B60BB /* README.md in Resources */, A51044182B493675006B60BB /* README.md in Resources */,
A5573EDB2B317C0000D98EC0 /* Assets.xcassets in Resources */, A5573EDB2B317C0000D98EC0 /* Assets.xcassets in Resources */,
); );
...@@ -1213,6 +1240,7 @@ ...@@ -1213,6 +1240,7 @@
A5FD63C52B637EE000D1D9DA /* YHSubmitAllInfoCheckView.swift in Sources */, A5FD63C52B637EE000D1D9DA /* YHSubmitAllInfoCheckView.swift in Sources */,
A5ACE9442B4564F7002C94D2 /* Array+Extension.swift in Sources */, A5ACE9442B4564F7002C94D2 /* Array+Extension.swift in Sources */,
0468D4282B50D4AF00CFB916 /* YHPrivacyAlertView.swift in Sources */, 0468D4282B50D4AF00CFB916 /* YHPrivacyAlertView.swift in Sources */,
0468D47A2B68A08600CFB916 /* YHAddressPickViewTableViewCell.swift in Sources */,
0468D4342B56587B00CFB916 /* YHOSSManager.swift in Sources */, 0468D4342B56587B00CFB916 /* YHOSSManager.swift in Sources */,
A5C382D12B5FAAB300C5E65C /* YHContactItemModel.swift in Sources */, A5C382D12B5FAAB300C5E65C /* YHContactItemModel.swift in Sources */,
A5ACE9522B4564F7002C94D2 /* YHServiceCenterMainViewController.swift in Sources */, A5ACE9522B4564F7002C94D2 /* YHServiceCenterMainViewController.swift in Sources */,
...@@ -1240,6 +1268,7 @@ ...@@ -1240,6 +1268,7 @@
0468D4432B61F76800CFB916 /* YHMainApplicantInformationViewController.swift in Sources */, 0468D4432B61F76800CFB916 /* YHMainApplicantInformationViewController.swift in Sources */,
0468D4262B495A5400CFB916 /* YHPickPhoneAddressViewController.swift in Sources */, 0468D4262B495A5400CFB916 /* YHPickPhoneAddressViewController.swift in Sources */,
A5C5B32D2B57D66000A7C5D1 /* YHBbxCollectionViewCell.swift in Sources */, A5C5B32D2B57D66000A7C5D1 /* YHBbxCollectionViewCell.swift in Sources */,
0468D47C2B68A08600CFB916 /* YHAddressPickView.swift in Sources */,
A5FD63D82B6603A500D1D9DA /* YHScoreResultView.swift in Sources */, A5FD63D82B6603A500D1D9DA /* YHScoreResultView.swift in Sources */,
A5ACE9342B4564F7002C94D2 /* YHAllApiName.swift in Sources */, A5ACE9342B4564F7002C94D2 /* YHAllApiName.swift in Sources */,
A5ACE9502B4564F7002C94D2 /* YhCacheTool.swift in Sources */, A5ACE9502B4564F7002C94D2 /* YhCacheTool.swift in Sources */,
...@@ -1288,7 +1317,9 @@ ...@@ -1288,7 +1317,9 @@
042FBBBD2B63519C00F9DE23 /* YHFormItemSelectSheetCell.swift in Sources */, 042FBBBD2B63519C00F9DE23 /* YHFormItemSelectSheetCell.swift in Sources */,
A5FD63C32B63438A00D1D9DA /* YHContactMainItemStatusModel.swift in Sources */, A5FD63C32B63438A00D1D9DA /* YHContactMainItemStatusModel.swift in Sources */,
041B52982B5E8EA0007EBCEB /* YHFamilyMemberFormVC.swift in Sources */, 041B52982B5E8EA0007EBCEB /* YHFamilyMemberFormVC.swift in Sources */,
0468D47B2B68A08600CFB916 /* YHAddressPresentAnimated.swift in Sources */,
A5C382CF2B5F9A9100C5E65C /* YHServiceCenterMainViewModel.swift in Sources */, A5C382CF2B5F9A9100C5E65C /* YHServiceCenterMainViewModel.swift in Sources */,
0468D4782B68A08600CFB916 /* YHAddressModel.swift in Sources */,
A5C5B2EA2B4ECA4D00A7C5D1 /* YHDavidModel0.swift in Sources */, A5C5B2EA2B4ECA4D00A7C5D1 /* YHDavidModel0.swift in Sources */,
A5ACE9572B4564F7002C94D2 /* YHBasicContentView.swift in Sources */, A5ACE9572B4564F7002C94D2 /* YHBasicContentView.swift in Sources */,
042FBBC92B64DC8900F9DE23 /* YHFormItemExpireDateCell.swift in Sources */, 042FBBC92B64DC8900F9DE23 /* YHFormItemExpireDateCell.swift in Sources */,
...@@ -1298,6 +1329,7 @@ ...@@ -1298,6 +1329,7 @@
A5ACE9582B4564F7002C94D2 /* YHNoDataTipView.swift in Sources */, A5ACE9582B4564F7002C94D2 /* YHNoDataTipView.swift in Sources */,
042FBBBB2B62806D00F9DE23 /* YHCertificateViewController.swift in Sources */, 042FBBBB2B62806D00F9DE23 /* YHCertificateViewController.swift in Sources */,
042FBBC72B64CFBF00F9DE23 /* YHChildInitialInfoVC.swift in Sources */, 042FBBC72B64CFBF00F9DE23 /* YHChildInitialInfoVC.swift in Sources */,
0468D4792B68A08600CFB916 /* YHAddressViewController.swift in Sources */,
A5ACE9562B4564F7002C94D2 /* YHBouncesContentView.swift in Sources */, A5ACE9562B4564F7002C94D2 /* YHBouncesContentView.swift in Sources */,
042FBBCB2B65058000F9DE23 /* YHChildBasicInfoVC.swift in Sources */, 042FBBCB2B65058000F9DE23 /* YHChildBasicInfoVC.swift in Sources */,
A5ACE9302B4564F7002C94D2 /* Algorithm.swift in Sources */, A5ACE9302B4564F7002C94D2 /* Algorithm.swift in Sources */,
......
//
// YHDatePickView.swift
// galaxy
//
// Created by EDY on 2024/1/30.
// Copyright © 2024 https://www.galaxy-immi.com. All rights reserved.
//
import Foundation
struct YHCountryModel {
var provincesArray: [YHProvincesModel] = []
init(dic: [String:[String:[String]]]) {
for (key,value) in dic {
let model = YHProvincesModel(name: key, dic: value)
provincesArray.append(model)
provincesArray.sort { a, b in
a.provincesName < b.provincesName
}
}
}
}
struct YHProvincesModel {
var provincesName: String = ""
var cityArray: [YHCityModel] = []
init(name : String ,dic:[String: [String]]) {
provincesName = name
for (key,value) in dic {
let model = YHCityModel(name: key, Arr: value)
cityArray.append(model)
cityArray.sort { a, b in
a.cityName < b.cityName
}
}
}
}
struct YHCityModel {
var cityName: String = ""
var areaArray: [String] = []
init(name: String,Arr: [String]) {
cityName = name
for str in Arr {
areaArray.append(str)
}
areaArray.sort(by: <)
}
}
//
// YHDatePickView.swift
// galaxy
//
// Created by EDY on 2024/1/30.
// Copyright © 2024 https://www.galaxy-immi.com. All rights reserved.
//
import UIKit
/// view的三种状态,控制tableView展示效果与数据
enum YHLocationPickViewTableViewType {
case provinces //省份
case city //城市
case area //地区
}
class YHAddressPickView: UIView {
/// 返回数据回调
public var backLocationString: ((String,String,String,String) -> Void)?
/// 退出回调
public var backOnClickCancel: (() -> Void)?
/// title选中以及下滑线颜色
public var selectColor: UIColor
private var tableViewType: YHLocationPickViewTableViewType = .provinces {
didSet {
switch tableViewType {
case .provinces:
/// 选择省份时,有上面的热门城市view.没有滚动选择type的titleScrollView.没有已选择label.
self.tableView.tableHeaderView = tableViewHeaderView
self.tableView.frame = CGRect(x: 0, y: 42, width: UIScreen.main.bounds.width, height: 458)
self.titleSV.isHidden = true
self.leftLabel.isHidden = true
/// 将所有选中数据清空
self.provincesModel = nil
self.selectedProvince = ""
self.selectedCity = ""
self.selectedArea = ""
self.cityModel = nil
// 将titleSV中所有button的title重置
// 并将第一个button设置为选中状态,已保证选择城市后button下的横线有滚动效果.
for button in buttonArr {
button.setTitle("请选择", for: .normal)
button.isSelected = false
if button.tag == 0 {
button.isSelected = true
}
}
self.underLine.center = CGPoint(x: self.buttonArr[1].center.x, y: self.underLine.center.y)
self.dataArray = locationModel?.provincesArray.map({ model in
model.provincesName
})
self.tableView.reloadData()
case .city:
/// 选择城市时没有热门城市view,并将titleSV显示出来
self.tableView.tableHeaderView = UIView()
self.tableView.frame = CGRect(x: 0, y: 136, width: UIScreen.main.bounds.width, height: 367)
self.titleSV.isHidden = false
self.leftLabel.isHidden = false
/// 将省份选择保留,将城市与地区数据清空
self.selectedCity = ""
self.selectedArea = ""
self.cityModel = nil
/// 通过修改titleSV中button的选中状态来修改它的颜色
for button in buttonArr {
button.isSelected = false
if button.tag != 0 {
button.setTitle("请选择", for: .normal)
}
if button.tag == 1 {
button.isSelected = true
}
}
/// 滚动titleSV中button下滚动的Line
UIView.animate(withDuration: 0.3, animations: {() -> Void in
self.underLine.center = CGPoint(x: self.buttonArr[1].center.x, y: self.underLine.center.y)
})
self.dataArray = provincesModel?.cityArray.map({ model in
model.cityName
})
self.tableView.reloadData()
case .area:
/// 选择地区时没有上方热门城市View,有titleSV
self.tableView.tableHeaderView = UIView()
self.tableView.frame = CGRect(x: 0, y: 136, width: UIScreen.main.bounds.width, height: 367)
self.titleSV.isHidden = false
self.leftLabel.isHidden = false
/// 通过修改titleSV中button的选中状态来修改它的颜色
for button in buttonArr {
button.isSelected = false
if button.tag == 2 {
button.isSelected = true
}
}
/// 滚动titleSV中button下滚动的Line
UIView.animate(withDuration: 0.3, animations: {() -> Void in
self.underLine.center = CGPoint(x: self.buttonArr[2].center.x, y: self.underLine.center.y)
})
self.dataArray = cityModel?.areaArray
self.tableView.reloadData()
}
}
}
/// titleSV上的三个button,通过array保存,更好操作
private var buttonArr = [UIButton]()
/// 已选中的省份
private var selectedProvince = ""{
didSet {
/// 当选中赋值时,将titleSV上第一个button.title改为省份名
for button in buttonArr where button.tag == 0 {
button.setTitle(selectedProvince, for: .normal)
}
}
}
/// 已选中城市
private var selectedCity = ""{
didSet {
/// 当选中赋值时,将titleSV上第二个button.title改为城市名
for button in buttonArr where button.tag == 1 {
button.setTitle(selectedCity, for: .normal)
}
}
}
private var selectedArea = ""
/// 总城市数据
private var locationModel: YHCountryModel?
/// 省份数据
private var provincesModel: YHProvincesModel?
/// 城市数据
private var cityModel: YHCityModel?
/// 当前tableView使用的数据源
private var dataArray: [String]?
private let titleLabel: UILabel = {
let label = UILabel(frame: CGRect(x: (UIScreen.main.bounds.width - 100) / 2, y: 9, width: 100, height: 24))
label.textColor = UIColor(red: 51/255, green: 51/255, blue: 51/255, alpha: 1)
label.text = "选择国家地区"
label.textAlignment = .center
label.font = UIFont.boldSystemFont(ofSize: 16)
return label
}()
private let rightCancelButton: UIButton = {
let button = UIButton(frame: CGRect(x: UIScreen.main.bounds.width - 42, y: 11, width: 18, height: 18))
button.setImage(UIImage(named: "BaseVC_cancel"), for: .normal)
return button
}()
private let leftLabel: UILabel = {
let label = UILabel(frame: CGRect(x: 24, y: 43, width: 40, height: 18))
label.text = "已选择"
label.textColor = UIColor(red: 102/255, green: 102/255, blue: 102/255, alpha: 1)
label.font = UIFont.systemFont(ofSize: 12)
label.isHidden = true
return label
}()
/// 热门城市数组,可修改,若修改数量需要修改下方tableViewHeaderView.frame;若修改城市需要修改onClickHotCity()方法来实现点击跳转功能
private let hotCityArray = ["北京","上海","广州","深圳","杭州","南京","苏州","天津","武汉","长沙","重庆","成都"]
private lazy var tableViewHeaderView: UIView = {
let view = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 160))
let label = UILabel(frame: CGRect(x: 24, y: 0, width: 50, height: 18))
label.textColor = UIColor(red: 102/255, green: 102/255, blue: 102/255, alpha: 1)
label.font = UIFont.systemFont(ofSize: 12)
label.text = "热门城市"
view.addSubview(label)
for i in 0..<12 {
let button: UIButton = UIButton(frame: CGRect(x: CGFloat(24 + 80 * (i % 4)), y: CGFloat(28 + 40 * (i / 4)), width: 80, height: 40))
button.setTitle(hotCityArray[i], for: .normal)
button.setTitleColor(UIColor(red: 102/255, green: 102/255, blue: 102/255, alpha: 1), for: .normal)
button.titleLabel?.font = UIFont.systemFont(ofSize: 12)
button.addTarget(self, action: #selector(onClickHotCity(sender:)), for: .touchUpInside)
button.tag = i
view.addSubview(button)
}
return view
}()
/// 上方选择省份城市地区的滚动scrollView
private var titleSV: UIScrollView!
/// titleSV上button下的滚动线
private var underLine = UIView()
private var tableView = UITableView(frame: CGRect(x: 0, y:42 , width: UIScreen.main.bounds.width, height: 458))
init(frame: CGRect, selectColor: UIColor, selectedProvince: String? = nil, selectedCity: String? = nil) {
self.selectColor = selectColor
super.init(frame: frame)
initLocationData()
drawMyView()
setHotCityData(province: selectedProvince, city: selectedCity)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
private func drawMyView() {
self.backgroundColor = UIColor.white
buildTitleScrollView()
drawTableView()
self.addSubview(titleLabel)
self.addSubview(rightCancelButton)
rightCancelButton.addTarget(self, action: #selector(onClickCancelButton), for: .touchUpInside)
self.addSubview(leftLabel)
}
private func buildTitleScrollView() {
if titleSV != nil {
titleSV.removeFromSuperview()
}
buttonArr = []
titleSV = UIScrollView(frame: CGRect(x: 0, y: 72, width: UIScreen.main.bounds.width, height: 44))
self.underLine = UIView(frame: CGRect(x: 0, y: 40, width: 30, height: 2))
self.underLine.backgroundColor = selectColor
for i in 0..<3 {
let button = UIButton(frame: CGRect(x: 24 + CGFloat(i) * (UIScreen.main.bounds.width - 47) / 3, y: 0, width: UIScreen.main.bounds.width / 3, height: 44))
button.tag = Int(i)
if i == 1 {
button.isSelected = true
underLine.center.x = button.center.x
}
button.setTitle("请选择", for: .normal)
button.setTitleColor(UIColor(red: 51/255, green: 51/255, blue: 51/255, alpha: 1), for: .normal)
button.setTitleColor(selectColor, for: .selected)
button.titleLabel?.font = UIFont.systemFont(ofSize: 12)
button.titleLabel?.adjustsFontSizeToFitWidth = true
button.addTarget(self, action: #selector(onClickTitlebutton(sender:)), for: .touchUpInside)
buttonArr.append(button)
titleSV.addSubview(button)
}
titleSV.showsVerticalScrollIndicator = false
titleSV.addSubview(self.underLine)
titleSV.contentSize = CGSize(width: UIScreen.main.bounds.width, height: 44)
titleSV.isHidden = true
self.addSubview(titleSV)
}
private func drawTableView() {
self.addSubview(tableView)
tableView.delegate = self
tableView.dataSource = self
tableView.separatorStyle = .none
tableView.tableHeaderView = tableViewHeaderView
tableView.showsVerticalScrollIndicator = false
tableView.register(YHAddressPickViewTableViewCell.self, forCellReuseIdentifier: YHAddressPickViewTableViewCell.identifier)
tableView.register(YHAddressPickViewFirstTableViewCell.self, forCellReuseIdentifier: YHAddressPickViewFirstTableViewCell.identifier)
}
/// 退出
@objc private func onClickCancelButton() {
if self.backOnClickCancel != nil {
backOnClickCancel!()
}
}
@objc private func onClickHotCity(sender: UIButton) {
let hotProvince = ["北京市","上海市","广东省","广东省","浙江省","江苏省","江苏省","天津市","湖北省","湖南省","重庆市","四川省"]
let hotCity = ["北京市","上海市","广州市","深圳市","杭州市","南京市","苏州市","天津市","武汉市","长沙市","重庆市","成都市"]
let tag = sender.tag
setHotCityData(province: hotProvince[tag], city: hotCity[tag])
self.tableViewType = .area
}
/// 选择view.type
@objc private func onClickTitlebutton(sender: UIButton) {
guard !sender.isSelected else { return }
switch sender.tag {
case 0:
self.tableViewType = .provinces
case 1:
self.tableViewType = .city
default:
break
}
}
/// 点击热门城市中的城市
private func setHotCityData(province: String?,city: String?) {
if let province = province {
if let provincesModel = self.locationModel?.provincesArray.first(where: { model in
model.provincesName == province
}) {
self.provincesModel = provincesModel
self.selectedProvince = province
self.tableViewType = .city
if let city = city {
if let cityModel = self.provincesModel?.cityArray.first(where: { model in
model.cityName == city
}) {
self.cityModel = cityModel
self.selectedCity = city
self.tableViewType = .area
}
}
}
}
}
/// 从area.plist获取全部地区数据
private func initLocationData() {
guard let dic = NSDictionary(contentsOfFile: Bundle.main.path(forResource: "area", ofType: "plist") ?? "") as? [String:[String:[String]]] else {
return
}
locationModel = YHCountryModel(dic: dic)
dataArray = locationModel?.provincesArray.map({$0.provincesName})
}
}
// MARK: - tableViewDelegate
extension YHAddressPickView: UITableViewDelegate,UITableViewDataSource {
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 40
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return (self.dataArray?.count)! + 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row == 0 {
guard let cell = tableView.dequeueReusableCell(withIdentifier: YHAddressPickViewFirstTableViewCell.identifier) as? YHAddressPickViewFirstTableViewCell else {
return YHAddressPickViewFirstTableViewCell()
}
cell.label.text = "请选择"
return cell
}
guard let cell = tableView.dequeueReusableCell(withIdentifier: YHAddressPickViewTableViewCell.identifier) as? YHAddressPickViewTableViewCell else {
return YHAddressPickViewTableViewCell()
}
cell.label.text = self.dataArray?[indexPath.row - 1]
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
guard indexPath.row != 0 else { return }
switch tableViewType {
case .provinces:
/// 当前为选择省份状态时,保存选中省份,刷新状态为选择城市
selectedProvince = self.locationModel?.provincesArray[indexPath.row - 1].provincesName ?? ""
self.provincesModel = self.locationModel?.provincesArray.first(where: {$0.provincesName == selectedProvince})
self.tableViewType = .city
case .city:
/// 当前为选择城市状态时,保存选中城市,刷新状态为选择地区
selectedCity = (self.provincesModel?.cityArray[indexPath.row - 1])?.cityName ?? ""
self.cityModel = self.provincesModel?.cityArray.first(where: {$0.cityName == selectedCity})
self.tableViewType = .area
case .area:
/// 当前为选择地区状态时,保存选中地区,执行回调block.将选中数据回调
selectedArea = self.dataArray![indexPath.row - 1]
let selectLocation = selectedProvince + " " + selectedCity + " " + selectedArea
if backLocationString != nil {
backLocationString!(selectLocation,selectedProvince,selectedCity,selectedArea)
}
}
}
}
//
// YHDatePickView.swift
// galaxy
//
// Created by EDY on 2024/1/30.
// Copyright © 2024 https://www.galaxy-immi.com. All rights reserved.
//
import UIKit
class YHAddressPickViewTableViewCell: UITableViewCell {
static let identifier = "YHAddressPickViewTableViewCell"
let label: UILabel = {
let label = UILabel(frame: CGRect(x: 42, y: 8, width: 200, height: 24))
label.font = UIFont.systemFont(ofSize: 14)
label.textColor = UIColor(red: 51/255, green: 51/255, blue: 51/255, alpha: 1)
return label
}()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.selectionStyle = .none
drawMyView()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
private func drawMyView() {
self.addSubview(label)
}
}
class YHAddressPickViewFirstTableViewCell: UITableViewCell {
static let identifier = "YHAddressPickViewFirstTableViewCell"
let label: UILabel = {
let label = UILabel(frame: CGRect(x: 24, y: 11.5, width: 100, height: 17))
label.font = UIFont.systemFont(ofSize: 12)
label.textColor = UIColor(red: 102/255, green: 102/255, blue: 102/255, alpha: 1)
return label
}()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.selectionStyle = .none
drawMyView()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
private func drawMyView() {
self.addSubview(label)
}
}
//
// YHDatePickView.swift
// galaxy
//
// Created by EDY on 2024/1/30.
// Copyright © 2024 https://www.galaxy-immi.com. All rights reserved.
//
import UIKit
enum YHAddressPickerPresentAnimateType {
case present//被推出时
case dismiss//取消时
}
//EWAddressPickerViewController的推出和取消动画
class YHAddressPickerPresentAnimated: NSObject,UIViewControllerAnimatedTransitioning {
var type: YHAddressPickerPresentAnimateType = .present
init(type: YHAddressPickerPresentAnimateType) {
self.type = type
}
/// 动画时间
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return 0.3
}
/// 动画效果
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
switch type {
case .present:
guard let toVC: YHAddressViewController = transitionContext.viewController(forKey: .to) as? YHAddressViewController else {
return
}
let toView = toVC.view
let containerView = transitionContext.containerView
containerView.addSubview(toView!)
toVC.containV.transform = CGAffineTransform(translationX: 0, y: (toVC.containV.frame.height))
UIView.animate(withDuration: 0.25, animations: {
/// 背景变色
toVC.backgroundView.alpha = 1.0
/// addresspicker向上推出
toVC.containV.transform = CGAffineTransform(translationX: 0, y: -10)
}) { (_) in
UIView.animate(withDuration: 0.2, animations: {
/// transform初始化
toVC.containV.transform = CGAffineTransform.identity
}, completion: { (_) in
transitionContext.completeTransition(true)
})
}
case .dismiss:
guard let toVC: YHAddressViewController = transitionContext.viewController(forKey: .from) as? YHAddressViewController else {
return
}
UIView.animate(withDuration: 0.25, animations: {
toVC.backgroundView.alpha = 0.0
/// addresspicker向下推回
toVC.containV.transform = CGAffineTransform(translationX: 0, y: (toVC.containV.frame.height))
}) { (_) in
transitionContext.completeTransition(true)
}
}
}
}
//
// YHDatePickView.swift
// galaxy
//
// Created by EDY on 2024/1/30.
// Copyright © 2024 https://www.galaxy-immi.com. All rights reserved.
//
/*
let addressPicker = YHAddressViewController( selectProvince: "上海市", selectCity: "" )
/*** 可使用这种init方法自定制选中颜色,不填写selectColor默认颜色为UIColor(red: 79/255, green: 176/255, blue: 255/255, alpha: 1),蓝色
let addressPicker = EWAddressViewController(selectColor: UIColor.yellow)
*/
// 返回选择数据,地址,省,市,区
addressPicker.backLocationStringController = { (address,province,city,area) in
}
self.present(addressPicker, animated: true, completion: nil)
*/
import UIKit
///默认选中颜色
let defaultColor = UIColor(red: 79/255, green: 176/255, blue: 255/255, alpha: 1)
class YHAddressViewController: UIViewController {
public var backLocationStringController: ((String,String,String,String) -> Void)?
///title选中颜色
public var selectColor: UIColor
public var selectProvince: String?
public var selectCity: String?
public lazy var containV: YHAddressPickView = {
let view = YHAddressPickView(frame: CGRect(x: 0, y: UIScreen.main.bounds.height - 550, width: UIScreen.main.bounds.width, height: 550),selectColor: selectColor,selectedProvince: selectProvince,selectedCity: selectCity)
view.backOnClickCancel = {
[weak self] in
self?.onClickCancel()
}
/// 成功选择后将数据回调,并推出视图
view.backLocationString = {[weak self] (address,province,city,area) in
if self?.backLocationStringController != nil {
self?.backLocationStringController!(address,province,city,area)
self?.onClickCancel()
}
}
return view
}()
public var backgroundView:UIView = {
let view = UIView()
view.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.4)
return view
}()
init(selectColor: UIColor = defaultColor,selectProvince: String? = nil, selectCity: String? = nil) {
self.selectColor = selectColor
self.selectProvince = selectProvince
self.selectCity = selectCity
super.init(nibName: nil, bundle: nil)
self.view.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.3)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
drawMyView()
// Do any additional setup after loading the view.
}
private func drawMyView() {
self.view.insertSubview(self.backgroundView, at: 0)
self.providesPresentationContextTransitionStyle = true
self.definesPresentationContext = true
self.modalPresentationStyle = .custom//viewcontroller弹出后之前控制器页面不隐藏 .custom代表自定义
self.view.addSubview(self.containV)
// 转场动画代理
self.transitioningDelegate = self
}
///点击推出
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesBegan(touches, with: event)
let currentPoint = touches.first?.location(in: self.view)
if !self.containV.frame.contains(currentPoint ?? CGPoint()) {
self.dismiss(animated: true, completion: nil)
}
}
// MARK: onClick
@objc private func onClickCancel() {
self.dismiss(animated: true, completion: nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
// MARK: - 转场动画delegate
extension YHAddressViewController: UIViewControllerTransitioningDelegate {
/// 推入动画
func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
let animated = YHAddressPickerPresentAnimated(type: .present)
return animated
}
/// 推出动画
func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
let animated = YHAddressPickerPresentAnimated(type: .dismiss)
return animated
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment