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

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

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

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

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

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

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

 


import UIKit

class ViewController: UIViewController {

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

    lazy var menu: Menu = {
        var me = Menu.initMenu(size: CGSize(width: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,width: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,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?.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() } }

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

 

转载于:https://www.cnblogs.com/Bob-blogs/p/6589768.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值