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() } }

還沒有覺得這個下拉菜單是完美的,匆匆趕覺得是夠用,以后慢慢完善吧……

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM