swift 城市选择三级联动选择器

//项目

import UIKit

import SnapKit

class ChangeHomeAddressVC: BaseViewController,UIPickerViewDelegate,UIPickerViewDataSource{

var pickerDic:NSDictionary!

var provinceArray:NSArray!

var cityArray:NSArray!

var townArray:NSArray!

var selectedArray:NSArray!

override func viewDidLoad() {

super.viewDidLoad()

self.navigationItem.title = "更改家庭地址"

//右边导航键

self.navigationItem.rightBarButtonItem = UIBarButtonItem.init(title: "保存", style: UIBarButtonItemStyle.Plain, target: self, action: #selector(ChangeHomeAddressVC.changeAddressByPicker))

prepareUI()

addPickView()

getPickerData()

}

//屏幕点击事件

override func touchesBegan(touches: Set, withEvent event: UIEvent?) {

self.view.endEditing(true)

}


//点击了改变地址的view触发事件

func changeAddress() {

print("点击了改变地址的view")

if cityPickView.frame.origin.y < UIScreen.mainScreen().bounds.height - 204{

print("点击过了")

}else {

likeToolView.hidden = false

cityPickView.hidden = false

UIView.animateWithDuration(1.25, delay: 0.1, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue:0), animations: {

self.likeToolView.transform = CGAffineTransformTranslate(self.likeToolView.transform, 0, -204)

self.cityPickView.transform = CGAffineTransformTranslate(self.cityPickView.transform , 0, -204)

}, completion: nil)

}

}

//MARK:  -视图

func prepareUI() {

self.view.addSubview(topView)

topView.addSubview(labelView)

labelView.addSubview(addressLabel)

labelView.addSubview(cityLabel)

labelView.addSubview(districtLabel)

topView.addSubview(fengGeLine)

topView.addSubview(AddressTextField)

labelView.snp_makeConstraints { (make) in

make.trailing.equalTo(topView.snp_trailing)

make.leading.equalTo(topView.snp_leading)

make.top.equalTo(topView.snp_top)

make.height.equalTo(44)

}

cityLabel.snp_makeConstraints { (make) in

make.centerY.equalTo(addressLabel.snp_centerY)

make.leading.equalTo(addressLabel.snp_trailing).offset(5)

}

districtLabel.snp_makeConstraints { (make) in

make.centerY.equalTo(addressLabel.snp_centerY)

make.leading.equalTo(cityLabel.snp_trailing).offset(5)

}

AddressTextField.snp_makeConstraints { (make) in

make.top.equalTo(fengGeLine.snp_bottom).offset(20)

make.leading.equalTo(fengGeLine.snp_leading)

make.trailing.equalTo(fengGeLine.snp_trailing)

}

fengGeLine.snp_makeConstraints { (make) in

make.top.equalTo(labelView.snp_bottom)

make.leading.equalTo(self.view.snp_leading).offset(10)

make.trailing.equalTo(self.view.snp_trailing).offset(-10)

make.height.equalTo(1)

}

addressLabel.snp_makeConstraints { (make) in

make.top.equalTo(labelView.snp_top).offset(15)

make.leading.equalTo(labelView.snp_leading).offset(10)

}

topView.snp_makeConstraints { (make) in

make.top.equalTo(self.view.snp_top).offset(10)

make.leading.equalTo(self.view.snp_leading)

make.trailing.equalTo(self.view.snp_trailing)

make.height.equalTo(99)

}

}

lazy var topView : UIView = {

let view = UIView()

view.backgroundColor = UIColor.whiteColor()

return view

}()

lazy var labelView : UIView  = {

let view = UIView()

view.backgroundColor = UIColor.whiteColor()

let addressLabelGesture = UITapGestureRecognizer.init(target: self, action: #selector(ChangeHomeAddressVC.changeAddress))

view.addGestureRecognizer(addressLabelGesture)

return view

}()

//省

lazy var addressLabel : UILabel = {

let label = UILabel()

label.text = "省市区"

label.font = UIFont.systemFontOfSize(14)

label.textColor = UIColor.init(red: 165.0/255.0, green: 165.0/255.0, blue: 165.0/255.0, alpha: 1.0)

return label

}()

//市

lazy var cityLabel : UILabel = {

let label =  UILabel()

label.text = " "

label.font = UIFont.systemFontOfSize(14)

label.textColor = UIColor.init(red: 165.0/255.0, green: 165.0/255.0, blue: 165.0/255.0, alpha: 1.0)

return label

}()

//区

lazy var districtLabel : UILabel = {

let label =  UILabel()

label.text = " "

label.font = UIFont.systemFontOfSize(14)

label.textColor = UIColor.init(red: 165.0/255.0, green: 165.0/255.0, blue: 165.0/255.0, alpha: 1.0)

return label

}()

lazy var fengGeLine : UIView = {

let line = UIView()

line.backgroundColor = UIColor.init(red: 240.0/255.0, green: 240.0/255.0, blue: 240.0/255.0, alpha: 1.0)

return line

}()

lazy var AddressTextField : UITextField = {

let text = UITextField()

text.placeholder = "详细地址(请精确房门号)"

text.textColor = UIColor.init(red: 51.0/255.0, green: 51.0/255.0, blue: 51.0/255.0, alpha: 1.0)

return text

}()

override func didReceiveMemoryWarning() {

super.didReceiveMemoryWarning()

// Dispose of any resources that can be recreated.

}

//MARK: -添加地址选择器

func addPickView() {

self.view.addSubview(likeToolView)

likeToolView.addSubview(sureBtn)

likeToolView.addSubview(topViewLine)

likeToolView.addSubview(cancelBtn)

self.view.addSubview(cityPickView)

cityPickView.snp_makeConstraints { (make) in

make.top.equalTo(likeToolView.snp_bottom)

make.leading.equalTo(likeToolView.snp_leading)

make.trailing.equalTo(likeToolView.snp_trailing)

make.height.equalTo(160)

}

cancelBtn.snp_makeConstraints { (make) in

make.centerY.equalTo(likeToolView.snp_centerY)

make.leading.equalTo(likeToolView.snp_leading).offset(20)

}

sureBtn.snp_makeConstraints { (make) in

make.centerY.equalTo(likeToolView.snp_centerY)

make.trailing.equalTo(likeToolView.snp_trailing).offset(-20)

}

likeToolView.snp_makeConstraints { (make) in

make.top.equalTo(self.view.snp_bottom)

make.leading.equalTo(self.view.snp_leading)

make.trailing.equalTo(self.view.snp_trailing)

make.height.equalTo(44)

}

topViewLine.snp_makeConstraints { (make) in

make.top.equalTo(likeToolView.snp_top)

make.leading.equalTo(likeToolView.snp_leading)

make.trailing.equalTo(likeToolView.snp_trailing)

make.height.equalTo(1)

}

}

lazy var likeToolView : UIView = {

let view = UIView()

//        view.backgroundColor = UIColor.init(red: 240.0/255.0, green: 240.0/255.0, blue: 240.0/255.0, alpha: 1.0)

view.backgroundColor = UIColor.whiteColor()

return view

}()

lazy var sureBtn : UIButton = {

let btn = UIButton()

btn.setTitle("确定", forState: UIControlState.Normal)

btn.setTitleColor(UIColor.init(r255: 41, g255: 196, b255: 117), forState: UIControlState.Normal)

btn.addTarget(self, action: #selector(ChangeHomeAddressVC.sureClick), forControlEvents: UIControlEvents.TouchUpInside)

btn.sizeToFit()

return btn

}()

lazy var cancelBtn : UIButton = {

let btn = UIButton()

btn.setTitle("取消", forState: UIControlState.Normal)

btn.setTitleColor(UIColor.init(r255: 165, g255: 165, b255: 165), forState: UIControlState.Normal)

btn.addTarget(self, action: #selector(ChangeHomeAddressVC.cancelBtnClick), forControlEvents: UIControlEvents.TouchUpInside)

return btn

}()

lazy var cityPickView : UIPickerView = {

let view = UIPickerView()

view.delegate = self

view.dataSource = self

//        view.backgroundColor = UIColor.yellowColor()

return view

}()

lazy var topViewLine : UIView = {

let view = UIView()

view.backgroundColor = UIColor.init(r255: 165, g255: 165, b255: 165)

return view

}()

//MARK: -pickerView

func getSubViews(view:UIView) {

for subView in view.subviews {

if subView.subviews.count != 0 {

self.getSubViews(subView)

}else {

if subView.frame.size.height <= 1 {

subView.backgroundColor = UIColor.init(r255: 41, g255: 196, b255: 117)

subView.alpha = 0.5

}

}

}

}

func sureClick() {

print("点击了确认按钮")

UIView.animateWithDuration(1.25, delay: 0.1, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue:0), animations: {

self.likeToolView.hidden = true

self.cityPickView.hidden = true

self.likeToolView.transform = CGAffineTransformIdentity

self.cityPickView.transform = CGAffineTransformIdentity

},completion: nil)

}

func cancelBtnClick() {

print("点击了取消按钮")

UIView.animateWithDuration(1.25, delay: 0.1, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue:0), animations: {

self.addressLabel.text = "省市区"

self.addressLabel.textColor = UIColor.init(red: 165.0/255.0, green: 165.0/255.0, blue: 165.0/255.0, alpha: 1.0)

}) { (_) in

self.likeToolView.hidden = true

self.cityPickView.hidden = true

self.likeToolView.transform = CGAffineTransformIdentity

self.cityPickView.transform = CGAffineTransformIdentity

}

}

//解析plist文件

func getPickerData(){

let path = NSBundle.mainBundle().pathForResource("Address", ofType: "plist")

self.pickerDic = NSDictionary.init(contentsOfFile: path!)

self.provinceArray = self.pickerDic.allKeys

self.selectedArray = self.pickerDic.objectForKey(self.pickerDic.allKeys[0]) as! NSArray

if (self.selectedArray.count > 0){

self.cityArray = self.selectedArray[0].allKeys

}

if (self.cityArray.count > 0){

self.townArray = self.selectedArray[0].objectForKey(self.cityArray[0]) as! NSArray

}

}

//MARK:-pickerView代理方法

func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {

return 3

}

func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView?) -> UIView {

self.getSubViews(pickerView)

var pickerLabel = UILabel()

pickerLabel = UILabel.init()

//        pickerLabel.font = UIFont(name: "Helvetica", size: 8)

pickerLabel.font = UIFont.systemFontOfSize(16)

pickerLabel.adjustsFontSizeToFitWidth = true

pickerLabel.textAlignment = .Left

pickerLabel.backgroundColor = UIColor.clearColor()

pickerLabel.text = self.pickerView(pickerView, titleForRow: row, forComponent: component)

return pickerLabel

}

func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {

if (component == 0) {

return self.provinceArray.count;

} else if (component == 1) {

return self.cityArray.count

} else {

return self.townArray.count

}

}

func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {

if (component == 0) {

//return [self.provinceArray objectAtIndex:row];

return self.provinceArray[row] as? String

} else if (component == 1) {

return self.cityArray[row] as? String;

} else {

return self.townArray[row] as? String;

}

}

func pickerView(pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat {

if (component == 0) {

return 110;

} else if (component == 1) {

return 100;

} else {

return 110;

}

}

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

if (component == 0) {

self.selectedArray = self.pickerDic.objectForKey(self.provinceArray[row]) as! NSArray

if (self.selectedArray.count > 0) {

self.cityArray = self.selectedArray[0].allKeys

} else {

self.cityArray = nil;

}

if (self.cityArray.count > 0) {

self.townArray = self.selectedArray[0].objectForKey(self.cityArray[0]) as! NSArray

} else {

self.townArray = nil;

}

pickerView.selectRow(0, inComponent: 1, animated: true)

}

pickerView.selectedRowInComponent(1)

pickerView.reloadComponent(1)

pickerView.selectedRowInComponent(2)

if (component == 1) {

if (self.selectedArray.count > 0 && self.cityArray.count > 0) {

self.townArray = self.selectedArray[0].objectForKey(self.cityArray[row]) as! NSArray

} else {

self.townArray = nil;

}

pickerView.selectRow(0, inComponent: 2, animated: true)

}

pickerView.reloadComponent(2)

//        provinceBtn.setTitle(self.provinceArray[self.myPicker.selectedRowInComponent(0)] as? String, forState:.Normal)

addressLabel.text = (self.provinceArray[self.cityPickView.selectedRowInComponent(0)] as? String)!

cityLabel.text =  (self.cityArray[self.cityPickView.selectedRowInComponent(1)] as? String)!

districtLabel.text =  (self.townArray[self.cityPickView.selectedRowInComponent(2)]as? String)!

addressLabel.textColor = UIColor.init(red: 51.0/255.0, green: 51.0/255.0, blue: 51.0/255.0, alpha: 1.0)

cityLabel.textColor = UIColor.init(red: 51.0/255.0, green: 51.0/255.0, blue: 51.0/255.0, alpha: 1.0)

districtLabel.textColor = UIColor.init(red: 51.0/255.0, green: 51.0/255.0, blue: 51.0/255.0, alpha: 1.0)

}

}

效果图如下


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,324评论 5 476
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,303评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,192评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,555评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,569评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,566评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,927评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,583评论 0 257
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,827评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,590评论 2 320
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,669评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,365评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,941评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,928评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,159评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,880评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,399评论 2 342

推荐阅读更多精彩内容