美文网首页
自定义下拉选择框

自定义下拉选择框

作者: 菲特峰 | 来源:发表于2020-09-17 15:27 被阅读0次

    效果图


    image.png

    使用方法

       let menuV =  LFPopMenuView.init(frame: CGRect.init(x: 15, y: 0, width: 80, height: 44), dataSource: ["aaa","bbb","ccc"])
            menuV.rowHeight = 30
            self.navigationController?.navigationBar.addSubview(menuV)
    

    上代码

    //
    //  LFPopMenuView.swift
    //  PXSSwift
    //
    //  Created by Pro on 2020/5/28.
    //  Copyright © 2020 刘刘峰. All rights reserved.
    //
    
    import UIKit
    
    class LFPopMenuView: UIView,UITableViewDelegate,UITableViewDataSource {
        
        var block : LFIndexCallBack?
        
        func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
            return self.rowHeight
        }
        
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return self.dataSource?.count ?? 0
        }
        
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cellId = "cell"
            var cell = tableView.dequeueReusableCell(withIdentifier: cellId)
            if cell == nil {
                cell = UITableViewCell.init(style: UITableViewCell.CellStyle.default, reuseIdentifier: cellId)
            }
            cell?.textLabel?.text = self.dataSource?[indexPath.row]
            cell?.textLabel?.font = textFontSize
            return cell!
            
        }
        
        func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            self.title = self.dataSource?[indexPath.row]
            self.dismiss()
            if self.block != nil {
                self.block!(indexPath.row)
            }
            
        }
        
        
        //MARK:-属性
        
        //标题
        var title:String? {
            didSet {
                self.titleLab.text = self.title
            }
        }
        //标题颜色
        var titleColor:UIColor?
        
        var titleFontSize:UIFont = UIFont.systemFont(ofSize: 12)
        var textFontSize:UIFont = UIFont.systemFont(ofSize: 12)
    
        var rowHeight:CGFloat = 44 {
            didSet {
                self.tableView.rowHeight = self.rowHeight
    
            }
        }
        
        var dataSource:[String]?{
            didSet {
                self.tableViewHeight = (self.dataSource?.count.cgFloat)! * self.rowHeight
            }
        }
        var tableViewHeight:CGFloat = 100
        
        var isDirectionUp:Bool?
        
        
        //MARK:-控件
        
        lazy var titleLab:UILabel = {
            let lab = UILabel()
            lab.text = "请选择"
            lab.font = self.titleFontSize
            
            
            return lab
        }()
        lazy var rightImageView:UIImageView = {
            let imageV = UIImageView()
            imageV.image = UIImage.init(named: "list_icon_drfault")
            return imageV
        }()
        
        lazy var maskBtn:UIButton = {
            let btn = UIButton()
            btn.backgroundColor = UIColor.clear
            btn.clipsToBounds = true
            btn.addTarget(self, action: #selector(show), for: UIControl.Event.touchUpInside)
            return btn
        }()
        
        
        lazy var backgroundBtn:UIButton = {
            let btn = UIButton()
            btn.backgroundColor = UIColor.clear
            btn.frame = UIScreen.main.bounds
            btn.addTarget(self, action: #selector(dismiss), for: UIControl.Event.touchUpInside)
            
            return btn
        }()
        
        
        lazy var tableView : UITableView = {
            let tab = UITableView.init(frame: .zero, style: UITableView.Style.plain)
            tab.tableFooterView = UIView()
            tab.rowHeight = rowHeight
            tab.delegate = self
            tab.dataSource = self
            tab.backgroundColor = UIColor.white
            tab.layer.shadowOffset = CGSize.init(width: 4, height: 4)
            tab.layer.shadowColor = UIColor.lightGray.cgColor
            tab.layer.shadowOpacity = 0.8
            tab.layer.shadowRadius = 4
            tab.layer.borderColor = UIColor.gray.cgColor
            tab.layer.borderWidth = 0.5
            tab.layer.cornerRadius = self.cornerRadius
            tab.separatorInset = UIEdgeInsets.init(top: 0, left: 5, bottom: 0, right: 5)
            
            
            return tab
        }()
        
        
        convenience init(frame:CGRect,dataSource:[String]) {
            self.init(frame: frame)
            self.dataSource = dataSource
    
            
            self.setupUI()
        }
        
        
        
        func setupUI(){
    //        self.cornerRadius = 5
    //        self.borderWidth = 1
    //        self.borderColor = AppColor.lineColor
            
    
            
            self.addSubview(self.rightImageView)
            self.addSubview(self.titleLab)
            self.addSubview(self.maskBtn)
            
            self.rightImageView.snp_makeConstraints { (make) in
                make.centerY.equalTo(self);
                make.right.equalTo(self.snp_right).offset(-10);
            }
            
            self.titleLab.snp_makeConstraints { (make) in
                make.left.equalTo(self.snp_left).offset(10);
                make.right.equalTo(self.rightImageView)
                make.centerY.equalToSuperview()
                make.height.equalTo(20)
                
            }
            self.maskBtn.snp_makeConstraints { (make) in
                make.left.right.top.bottom.equalTo(self);
            }
            
            
            
        }
        
    }
    
    extension LFPopMenuView {
        @objc func show () {
            kkwindow?.addSubview(self.backgroundBtn)
            kkwindow?.addSubview(self.tableView)
            // 获取按钮在屏幕中的位置
            let frame = self.convert(self.bounds, to: kkwindow)
            let tableY = frame.origin.y + frame.size.height
            var tableViewFrame:CGRect = .zero
            tableViewFrame.size.width = frame.size.width
            tableViewFrame.size.height = self.tableViewHeight
            tableViewFrame.origin.x = frame.origin.x
            
            if tableY + self.tableViewHeight < UIScreen.main.bounds.size.height {
                tableViewFrame.origin.y = tableY
                self.isDirectionUp = false
            }else {
                tableViewFrame.origin.y = frame.origin.y - tableViewHeight
                self.isDirectionUp = true
            }
            
            self.tableView.frame = CGRect.init(x: tableViewFrame.origin.x, y: tableViewFrame.origin.y + ( self.isDirectionUp == true ? tableViewHeight:0), width: tableViewFrame.size.width, height: 0)
            
            UIView.animate(withDuration: 0.25) {
                
                self.rightImageView.transform = CGAffineTransform.init(rotationAngle: self.isDirectionUp == true ? (CGFloat(-Double.pi/2)):(CGFloat(Double.pi/2)))
                    
    //                CGAffineTransformRotate(weakSelf.rightImageView.transform,self.isDirectionUp?-M_PI/2:M_PI/2);
    
                self.tableView.frame = CGRect.init(x: tableViewFrame.origin.x, y: tableViewFrame.origin.y, width: tableViewFrame.size.width, height: tableViewFrame.size.height);
                
                
            }
            
            
        }
        
        @objc func dismiss() {
            
            UIView.animate(withDuration: 0.25, animations: {
                self.rightImageView.transform = CGAffineTransform.identity
    
                self.tableView.frame = CGRect.init(x: self.tableView.frame.origin.x, y: self.tableView.frame.origin.y + ( self.isDirectionUp == true ? self.tableViewHeight:0), width: self.tableView.frame.size.width, height: 0)
                
    
            }) { (bol) in
                self.backgroundBtn.removeFromSuperview()
                self.tableView.removeFromSuperview()
            }
    
        }
    }
    
    

    相关文章

      网友评论

          本文标题:自定义下拉选择框

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