第一种:一个比较好用的第三方:STPickerView
(地址:https://github.com/STShenZhaoliang/STPickerView)
在需要点击的地方添加以下代码:
let pickerAre = STPickerArea()
pickerAre.delegate = self
pickerAre.show()
//并实现代理方法:
func pickerArea(pickerArea: STPickerArea, province: String, city: String, area: String) {
adressTF.text = "\(province)\(city)\(area)"
UIView.animateWithDuration(0.25) {
self.scrollView.contentOffset = CGPointZero
}
}
即可实现以下效果
第二种:一个不错的demo(https://github.com/HelloYeah/ChooseLocation)
最近突然提出的需求就是这种样式,直接导入demo,桥接一下头文件,用swift方式调用一下代码就可以轻松实现
override func viewDidLoad() {
CitiesDataTool.sharedManager().requestGetData()
}
override func gestureRecognizerShouldBegin(gestureRecognizer:UIGestureRecognizer) -> Bool {
let point = gestureRecognizer.locationInView(gestureRecognizer.view)
if CGRectContainsPoint(self.chooseLocationView.frame, point) {
return false
}
return true
}
//点击空白处收回控制器
func tapCover(tap:UITapGestureRecognizer){
if (chooseLocationView.chooseFinish != nil) {
self.chooseLocationView.chooseFinish()
}
}
//MARK:属性
lazy var cover:UIView = {
let cover = UIView(frame: UIScreen.mainScreen().bounds)
// cover.backgroundColor = UIColor.whiteColor()
// cover.alpha = 0.5
cover.backgroundColor = UIColor.init(white: 1, alpha: 0.2)
UIApplication.sharedApplication().keyWindow?.addSubview(cover)
self.chooseLocationView = ChooseLocationView(frame: CGRect(x: 0, y: UIScreen.mainScreen().bounds.size.height - 300, width: UIScreen.mainScreen().bounds.size.width, height: 300))
cover.addSubview(self.chooseLocationView)
self.chooseLocationView.chooseFinish = { (AnyObject) -> () in
UIView.animateWithDuration(0.25, animations: {
self.adressTF.text = self.chooseLocationView.address
self.view.transform = CGAffineTransformIdentity
cover.hidden = true;
})
}
let tap = UITapGestureRecognizer(target: self, action: #selector(tapCover(_:)))
cover.addGestureRecognizer(tap)
tap.delegate = self;
cover.hidden = true;
return cover
}()
需要点击的地方添加:
UIView.animateWithDuration(0.25, animations: {
self.view.transform = CGAffineTransformMakeScale(0.95, 0.95)
})
self.cover.hidden = !self.cover.hidden;
self.chooseLocationView.hidden = self.cover.hidden;
实现效果: