UIPickView的使用

2017-10-20  本文已影响0人  lalala1112389

一 :预期效果 :点击按钮出现 UIPickerView 背景变为半透明效果 .
1 在按钮的点击事件中

//创建全局变量
var pickerMaskView = UIView()
var pickerDataArray = [6, 7, 7.5, 8, 8.5]
var matrix: NALLabelsMatrix!
func danJiaBtnClick(){
//调用显示pickerView的方法
self.showPickerView()
}
func showPickerView()
{
for view in pickerMaskView.subviews {
view.removeFromSuperview()
}

   //创建一个透明的UIView,添加到UIWindow上
    self.pickerMaskView.frame = UIScreen.main.bounds
    self.pickerMaskView.backgroundColor = UIColor.clear
    UIApplication.shared.keyWindow?.addSubview(self.pickerMaskView)

   //创建一个半透明的UIView,添加到pickerMaskView上
    let maskView = UIView.init(frame: self.pickerMaskView.bounds)
    maskView.backgroundColor = UIColor.init(red: 0, green: 0, blue: 0, alpha: 0.3)
    maskView.isUserInteractionEnabled = true
    self.pickerMaskView.addSubview(maskView)
    //maskView上添加手势的点击事件,点击maskView,隐藏pickView
    let tap = UITapGestureRecognizer.init(target: self, action: #selector(hidePickerView))
    maskView.addGestureRecognizer(tap)

    //创建可以添加PickView的UIView,添加到pickerMaskView上
    let operationView = UIView.init(frame: CGRect.init(x: 0, y: UIScreen.main.bounds.height-162-30, width: UIScreen.main.bounds.width, height: 30))
    operationView.backgroundColor = UIColor.gray
    self.pickerMaskView.addSubview(operationView)

    //将取消按钮以及确定按钮添加到operationView上
    let cancelBtn = UIButton.init(type: .custom)
    cancelBtn.frame = CGRect.init(x: 10, y: 5, width: 40, height: 20)
    cancelBtn.setTitle("取消", for: .normal)
    cancelBtn.addTarget(self, action: #selector(hidePickerView), for: .touchUpInside)
    operationView.addSubview(cancelBtn)
    
    let okBtn = UIButton.init(type: .custom)
    okBtn.frame = CGRect.init(x: UIScreen.main.bounds.width-50, y: 5, width: 40, height: 20)
    okBtn.setTitle("确定", for: .normal)
    okBtn.addTarget(self, action: #selector(selectedPickerView), for: .touchUpInside)
    operationView.addSubview(okBtn)
    
    self.pickerView = UIPickerView.init(frame: CGRect.init(x: 0, y: UIScreen.main.bounds.height-162, width: UIScreen.main.bounds.width, height: 162))
    self.pickerView.delegate = self
    self.pickerView.dataSource = self
    //设置pickerView的列数为一列显示
    self.pickerView.selectedRow(inComponent: 0)
    self.pickerView.backgroundColor = UIColor.white
    self.pickerMaskView.addSubview(self.pickerView

}
//隐藏pickView
func hidePickerView() {
self.pickerMaskView.removeFromSuperview()
}
\ 选择pickView
func selectedPickerView() {
var row = 0
// 得到当前的pickView显示的是第几行
row = self.pickerView.selectedRow(inComponent: 0)
self.danJiaBtn.setTitle("(self.pickerDataArray[row])", for: .normal)
self.hidePickerView()
}
\实现UIPickerView的代理方法
extension CalulatorViewController: UIPickerViewDelegate, UIPickerViewDataSource {
\ 定义pickerView的列数
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
\ pickerView一列显示几行
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return self.pickerDataArray.count
}
\ pickerView 每行显示的内容
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return "(self.pickerDataArray[row])"
}
\pickerView选择某一行以后所做的处理
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
// self.danJiaBtn.setTitle("(self.pickerDataArray[row])", for: .normal)
}
}
到此为止,从pickView选择的值可以显示到我们想要显示的控件上

上一篇 下一篇

猜你喜欢

热点阅读