• swift 第九课 用tableview 做一个下拉菜单Menu


         写到这里的时候,自己这个项目已经完成了一半左右,项目进度自己还是挺满意。今天又有一个新的布局,要实现个下拉菜单,刚开始写的时候,觉得会很容易,后来发现也是小错不断,

         我想自己限制的自己属于写博客的初期,主要是记录错误,和喜欢撸码的朋友共同进步……

         这个好多是借鉴别人博客的,免不了有搬代码的嫌疑,以后遇到在给他点赞吧……………………

         这个控件主要是封装带用,方便转移到其他的项目,所以就纯粹的用代码写了……

        最终呈现的效果,点击弹出,点击消失::::

        先呈现调用的方法 应该会比较好:


    import UIKit

    class ViewController: UIViewController {

        lazy var levelArr: Array<Any>? = {
            return ["全部","三甲医院","三乙医院","二甲医院","二乙医院","门诊","民营医院","体检机构","私营企业"]
        }()

        lazy var menu: Menu = {
            var me = Menu.initMenu(size: CGSize(UIScreen.main.bounds.size.width / 3,height:self.view.frame.size.height / 3))
            self.view.addSubview(me)
            return me
        }()
        

    /**
    菜单的弹出 和 收回
    */
        override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
           
            if self.menu.isShow! == false {
               
                let point = CGPoint(x:100,y: 100)
                self.menu.popupMenu(orginPoint:point, arr: self.levelArr!)
                
                self.menu.didSelectIndex = { [unowned self] (index:Int) in
                    
                    print( "选中--  (index) -行 -- (self.levelArr?[index])")
                }
            }else{
                
                self.menu.packUpMenu()
            }
        }
        
        
    /*
    vc 系统固有方法
    */
        override func viewDidLoad() {
            super.viewDidLoad()
        }

        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
        }
    }

        这里还是贴代码,到时看注释就好了……

      

    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)?
        /*
         加载动画效果
         */
        var isShow : Bool?
        var menuSize : CGSize?
    class func initMenu(size:CGSize)->Menu{ let frame = CGRect(x:0,y:0,size.width,height:size.height) let me = Menu.init(frame:frame) me.menuSize = size return me } override init(frame: CGRect) {
    /**
    这时候 frame 的 height 设置为 0 ,是为了加载缓慢弹出的动画……
    */ let initialFrame
    = CGRect(x:frame.origin.x,y:frame.origin.y,frame.size.width,height:0.0) super.init(frame: initialFrame) self.backgroundColor = UIColor.black menuTab = UITableView.init(frame: CGRect(x:0,y:0,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?.textLabel?.text = menuArr?[indexPath.row] as? String cell?.textLabel?.textAlignment = .center cell?.textLabel?.font = UIFont.systemFont(ofSize: 11.0) return cell! } func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat{ return 20 } 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() } }

    还没有觉得这个下拉菜单是完美的,匆匆赶觉得是够用,以后慢慢完善吧……

  • 相关阅读:
    算法(Algorithms)第4版 练习 1.3.4
    算法(Algorithms)第4版 练习 1.3.5
    算法(Algorithms)第4版 练习 1.3.6
    微服务迁移步骤建议
    xxxx interview问题解读与总结
    乐观锁与悲观锁——解决并发问题
    编码解码--三种常见字符编码简介:ASCII、Unicode和UTF-8
    编码解码--url编码解码
    分页缓存预加载算法
    Could not initialize class sun.awt.X11FontManager 异常问题
  • 原文地址:https://www.cnblogs.com/Bob-blogs/p/6589768.html
Copyright © 2020-2023  润新知