swift 城市選擇三級(jí)聯(lián)動(dòng)選擇器

//項(xiàng)目

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 = "更改家庭地址"

//右邊導(dǎo)航鍵

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

prepareUI()

addPickView()

getPickerData()

}

//屏幕點(diǎn)擊事件

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

self.view.endEditing(true)

}


//點(diǎn)擊了改變地址的view觸發(fā)事件

func changeAddress() {

print("點(diǎn)擊了改變地址的view")

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

print("點(diǎn)擊過了")

}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 = "省市區(qū)"

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

}()

//區(qū)

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 = "詳細(xì)地址(請(qǐng)精確房門號(hào))"

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("點(diǎn)擊了確認(rèn)按鈕")

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("點(diǎn)擊了取消按鈕")

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

self.addressLabel.text = "省市區(qū)"

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)

}

}

效果圖如下


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市卫旱,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌朋截,老刑警劉巖人断,帶你破解...
    沈念sama閱讀 211,290評(píng)論 6 491
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡鲤拿,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,107評(píng)論 2 385
  • 文/潘曉璐 我一進(jìn)店門署咽,熙熙樓的掌柜王于貴愁眉苦臉地迎上來近顷,“玉大人,你說我怎么就攤上這事宁否≈仙” “怎么了?”我有些...
    開封第一講書人閱讀 156,872評(píng)論 0 347
  • 文/不壞的土叔 我叫張陵家淤,是天一觀的道長异剥。 經(jīng)常有香客問我,道長絮重,這世上最難降的妖魔是什么冤寿? 我笑而不...
    開封第一講書人閱讀 56,415評(píng)論 1 283
  • 正文 為了忘掉前任,我火速辦了婚禮青伤,結(jié)果婚禮上督怜,老公的妹妹穿的比我還像新娘。我一直安慰自己狠角,他們只是感情好号杠,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,453評(píng)論 6 385
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著丰歌,像睡著了一般姨蟋。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上立帖,一...
    開封第一講書人閱讀 49,784評(píng)論 1 290
  • 那天眼溶,我揣著相機(jī)與錄音,去河邊找鬼晓勇。 笑死堂飞,一個(gè)胖子當(dāng)著我的面吹牛灌旧,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播绰筛,決...
    沈念sama閱讀 38,927評(píng)論 3 406
  • 文/蒼蘭香墨 我猛地睜開眼枢泰,長吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了铝噩?” 一聲冷哼從身側(cè)響起衡蚂,我...
    開封第一講書人閱讀 37,691評(píng)論 0 266
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎薄榛,沒想到半個(gè)月后讳窟,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 44,137評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡敞恋,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,472評(píng)論 2 326
  • 正文 我和宋清朗相戀三年丽啡,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片硬猫。...
    茶點(diǎn)故事閱讀 38,622評(píng)論 1 340
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡补箍,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出啸蜜,到底是詐尸還是另有隱情坑雅,我是刑警寧澤,帶...
    沈念sama閱讀 34,289評(píng)論 4 329
  • 正文 年R本政府宣布衬横,位于F島的核電站裹粤,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏蜂林。R本人自食惡果不足惜遥诉,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,887評(píng)論 3 312
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望噪叙。 院中可真熱鬧矮锈,春花似錦、人聲如沸睁蕾。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,741評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽子眶。三九已至瀑凝,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間臭杰,已是汗流浹背猜丹。 一陣腳步聲響...
    開封第一講書人閱讀 31,977評(píng)論 1 265
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留硅卢,地道東北人射窒。 一個(gè)月前我還...
    沈念sama閱讀 46,316評(píng)論 2 360
  • 正文 我出身青樓,卻偏偏與公主長得像将塑,于是被迫代替她去往敵國和親脉顿。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,490評(píng)論 2 348

推薦閱讀更多精彩內(nèi)容