美文网首页
UIPickView的使用

UIPickView的使用

作者: lalala1112389 | 来源:发表于2017-10-20 15:31 被阅读0次

一 :预期效果 :点击按钮出现 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选择的值可以显示到我们想要显示的控件上

相关文章

  • UIPickView的使用

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

  • UIPickView和UIDatePicker

    UIPickView和UIDatePicker UIPickView 样式 作用 常见用法 注意 显示数据 UID...

  • UI进阶1 UIDatePicker

    UIPickView和UIDatePicker 1.UIPickView什么时候用?• 通常在注册模块,当用户需要...

  • UIPickView和UIDatePicker

    一、介绍UIPickView和UIDatePicker 1.UIPickView什么时候用? •通常在注册模块,当...

  • UIPickView

    AZPickiew是基于UIPickView做的封装,用法相对简单 用法: github地址:https://gi...

  • UIPickView:点餐系统demo(基本框架)

    UIPickView: 1.遵守协议: UIPickerViewDataSource, UIPickerViewD...

  • UIPickView 去掉选择框

    UIPickView 去掉选择框 UIPickerView//去掉选择框

  • 2018-07-23

    //UIPickView 的分割线 (UIView *)pickerView:(UIPickerView *)pi...

  • iOS - UIPickView

    1,代理 2,简单Demo

  • iOS - UIPickView

    学习一个新类看这个类的头文件是唯一了解它的途径,进入头文件首先看它的父类和初始化方法: 它的父类是UIView它的...

网友评论

      本文标题:UIPickView的使用

      本文链接:https://www.haomeiwen.com/subject/ibkvuxtx.html