//項(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)
}
}
效果圖如下