swift3.0中弹出框

以前在OC中自定义过一个弹出框,基本原理都是一样的,都是利用tableView在一个view上面显示,然后在需要用到的地方,调用这个view.基本上就实现的想要的效果,在ipad里面开发还是有一个pop控制器,在iphone开发中的弹出框基本上都是自定义的 ,下面上干货
1.首先创建一个UIView类
类里面的代码是

import UIKit
class Menu: UIView,UITableViewDelegate,UITableViewDataSource {
    /*
     menu 的table 和 内容
     */
    var menuTab: UITableView?
    var menuArr : Array<Any>?
    let cellIdentifier = "cellID"
    /*
     menu 选中行的方法
     */
    var didSelectIndex:((_ index:Int)->Void)?//这个闭包用来传值,点击了某一行就会将这一行的row传到外部
    /*
     加载动画效果
     */
    var isShow : Bool?
    var menuSize : CGSize?

//初始化类方法,也是调用弹出框的入口
    class func initMenu(size:CGSize)->Menu{

        let frame = CGRect(x:0,y:0,width:size.width,height:size.height)
        let me = Menu.init(frame:frame)//初始化布局调用
        me.menuSize = size
        return me
    }

//UIView的初始化
    override init(frame: CGRect) {

        /**
         这时候 frame 的 height 设置为 0 ,是为了加载缓慢弹出的动画……
         */
        let  initialFrame = CGRect(x:frame.origin.x,y:frame.origin.y,width:frame.size.width,height:0.0)

        super.init(frame: initialFrame)
        self.backgroundColor = UIColor.black

        menuTab = UITableView.init(frame: CGRect(x:0,y:0,width:frame.size.width,height:0), style: .plain)
        menuTab?.tableFooterView = UIView.init()
        menuTab?.delegate = self
        menuTab?.dataSource = self

        addSubview(menuTab!)

        menuTab?.isHidden = true
        isHidden = true
        isShow = false
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    /*
     menu 弹出菜单的方法
     */
    func popupMenu(orginPoint:CGPoint,arr : Array<Any>){

        if self.isShow == true {
            return
        }
        self.isShow = true
        self.isHidden = false
        menuTab?.isHidden = false


        self.frame.origin = orginPoint
        self.menuArr = arr
        self.menuTab?.reloadData()

        self.superview?.bringSubview(toFront: self)
        menuTab?.frame.size.height = 0.0
        /**
         这里是 弹出的动画
         */
        UIView.animate(withDuration: 0.5, animations: {

            self.frame.size.height = (self.menuSize!.height)
            self.menuTab?.frame.size.height = (self.menuSize!.height)

        }) { (finish) in
        }
    }
    /**
     这里是收回菜单的方法
     */
    func packUpMenu() {

        if self.isShow == false {
            return
        }
        self.isShow = false

        UIView.animate(withDuration: 0.5, animations: {

            self.menuTab?.frame.size.height = 0.0
            self.frame.size.height = 0.0

        }) { (finish) in

            self.isHidden = true
            self.menuTab?.isHidden = true
        }
    }

    /*
     tableView delegate dataSource
     */

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        if ((menuArr?.count) != nil) {
            return (menuArr?.count)!
        }
        return 1
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{

        var cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier)
        if cell == nil {
            cell = UITableViewCell.init(style: .default, reuseIdentifier: cellIdentifier)
        }
        cell?.backgroundColor = #colorLiteral(red: 0.05882352963, green: 0.180392161, blue: 0.2470588237, alpha: 1)
        cell?.textLabel?.textColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
        cell?.textLabel?.text = menuArr?[indexPath.row] as? String
        cell?.textLabel?.textAlignment = .left
        cell?.textLabel?.font = UIFont.systemFont(ofSize: 11.0)

        return cell!
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat{
        return 25
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){

        tableView.deselectRow(at: indexPath, animated: true)
        /**
         这里是把 选中的 indexPath 传值出去 , 关闭menu列表
         */
        if (self.didSelectIndex != nil) {
            self.didSelectIndex!(indexPath.row)
        }
        self.packUpMenu()
    }
}

上面基本上将弹出框已经写好了,接下来就是在类控制器里面调用弹出框
2.

//MARK:下拉列表框选中时的方法.实现下面的代码调用到了下拉列表弹出框
    func selectBtn(){
        if self.menu.isShow! == false {

            let point = CGPoint(x:-5,y: 100)
            self.menu.popupMenu(orginPoint:point, arr: self.levelArr!)

            self.menu.didSelectIndex = { [unowned self] (index:Int) in

                print( "选中--  \(index) -行 -- \(String(describing: self.levelArr?[index]))")
                if index == 0 {
                    self.tf.text = "123"
                }else{
                    self.tf.text = ""
                }
            }
        }else{
            self.menu.packUpMenu()
        }
    }

好的,基本上都实现了想要的效果
https://github.com/13670242169/QYPSQLFMDBManager.git
https://github.com/13670242169/QYPWEIBO.git

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值