swift仿微信下拉菜單menu

先放預覽結(jié)果圖:


image.gif

以及swift文件:


xcode

接下來直接貼代碼加注釋:MenuView.swift

import UIKit

//自定義的Delegate瓣距,用于處理menu的單擊事件回調(diào)
protocol SwiftNoDataShowViewDelegate  {
    func  didClickSelectedRow(index: Int,title: String,menu: MenuView)
}

class MenuView: UIView, UITableViewDelegate, UITableViewDataSource{

    //顯示在menu每一行的文字
    var titleArray : [String]?
    //記時器连舍,負責顯示menu的動畫
    var timer: Timer?
    //記錄menu實際高度
    var resultFrame: CGRect?
    //menu內(nèi)容用tableView顯示
    var tableView: UITableView?
    var menuDelegate: SwiftNoDataShowViewDelegate?
    
    init(frame: CGRect, titleArray: [String]) {
        //初始化高度為0榛了,并保存實際高度用來做動畫
        super.init(frame: CGRect(x: frame.origin.x, y: frame.origin.y, width: frame.size.width, height: 0))
        self.resultFrame = frame
        self.titleArray = titleArray
        self.backgroundColor = UIColor.white
       
        tableView = UITableView(frame: CGRect(x: 0, y: 0, width: 0, height: 0), style: .plain)
        tableView!.delegate = self
        tableView!.dataSource = self
        tableView!.isScrollEnabled = false
        tableView!.layer.masksToBounds = true
        tableView!.layer.cornerRadius = 6
        tableView!.backgroundColor = UIColor.darkGray
        tableView!.separatorStyle = .none
        self.addSubview(tableView!)

        //設(shè)置記時器每0.01秒觸發(fā)一次
        timer = Timer.scheduledTimer(timeInterval: 0.01, target: self, selector: #selector(animationStart), userInfo: nil, repeats: true)
        timer!.fire()
    }
    
    @objc func animationStart() {
        //每0.01秒高度增加1%,動畫時間為1秒
        let height = (resultFrame?.size.height)!/100.0
        self.frame = CGRect(x: frame.origin.x, y: frame.origin.y, width: frame.size.width, height: frame.size.height + height)
        //每次高度變化都要刷新視圖
        setNeedsDisplay()
        //到達實際高度后停止動畫
        if(self.frame.size.height >= (self.resultFrame?.size.height)!){
            timer!.invalidate()
        }
    }
    
    override func draw(_ rect: CGRect) {

        //繪制小三角形
        guard let context = UIGraphicsGetCurrentContext() else {
            return
        }
        let drawingRect = bounds
        let path = CGMutablePath()
        //設(shè)立設(shè)置的三角形在中間,具體位置可以自定
        path.move(to: CGPoint(x: drawingRect.width/2, y: 0))
        path.addLine(to: CGPoint(x: drawingRect.width/2-8, y: 8))
        path.addLine(to: CGPoint(x: drawingRect.width/2+8, y: 8))
        context.setFillColor(UIColor.black.cgColor)
        context.addPath(path)
        context.fillPath()
        //設(shè)置tableView的frame,用于動畫效果
        tableView?.frame = CGRect(x: 0, y: 8, width: frame.size.width, height: frame.size.height-8)
    }
    
    //menu item的數(shù)量
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return (titleArray?.count)!
    }
    
    //headerview
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let label = UILabel(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 44))
        label.text = "HEADER"
        label.textColor = UIColor.blue
        label.textAlignment = .center
        let line = UIView(frame: CGRect(x: 0, y: 43.5, width: label.frame.size.width, height: 0.5))
        line.backgroundColor = UIColor.white
        label.addSubview(line)
        return label
    }
    //headerview高度
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 44
    }
    //顯示的cell,這里只顯示文字洞辣,具實際情況也可以加上圖片
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
        cell.textLabel?.text = "\(titleArray![indexPath.row])"
        cell.textLabel?.textColor = UIColor.white
        cell.textLabel?.font = UIFont.systemFont(ofSize: 12)
        cell.textLabel?.textAlignment = NSTextAlignment.left
        cell.backgroundColor = UIColor.clear
        let line = UIView(frame: CGRect(x: 0, y: 33.5, width: cell.frame.size.width, height: 0.5))
        line.backgroundColor = UIColor.white
        cell.addSubview(line)
        return cell
    }
    //單擊事件的Delegate回調(diào)
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        menuDelegate?.didClickSelectedRow(index: indexPath.row, title: self.titleArray![indexPath.row], menu: self)
    }
    //cell的高度
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 35
    }
    //移除menu的動畫,透明度漸變效果
    func removeMenu(){
        UIView.animate(withDuration: 1, animations: {
            self.alpha = 0
        }) { (finish) in
            self.removeFromSuperview()
        }
    }
    
    required init?(coder _: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

MenuViewController.swift

import UIKit

class MenuViewController: UIViewController,SwiftNoDataShowViewDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.view.backgroundColor = UIColor.white
        let btn = UIButton(frame: CGRect(x: 100, y: 70, width: 150, height: 50))
        btn.setTitle("menu", for: .normal)
        btn.setTitleColor(UIColor.blue, for: .normal)
        btn.layer.borderWidth = 1
        btn.layer.borderColor = UIColor.blue.cgColor
        btn.addTarget(self, action: #selector(add(_:)), for: .touchUpInside)
        view.addSubview(btn)
        
    }
    
    @objc func add(_ btn: UIButton) {
        //創(chuàng)建menuView
        let titleArray = ["title1","title2","title3","title4","title5","title6"]
        let menuView = MenuView(frame: CGRect(x: 50, y: 120, width: 250, height: titleArray.count*35+44+8), titleArray: titleArray)
        menuView.menuDelegate = self
        self.view.addSubview(menuView)
    }
    //單擊item的代理回調(diào)
    func didClickSelectedRow(index: Int, title: String, menu: MenuView) {
        print("\(index)----\(title)")
        menu.removeMenu()
    }

}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末昙衅,一起剝皮案震驚了整個濱河市扬霜,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌而涉,老刑警劉巖畜挥,帶你破解...
    沈念sama閱讀 217,734評論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異婴谱,居然都是意外死亡蟹但,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,931評論 3 394
  • 文/潘曉璐 我一進店門谭羔,熙熙樓的掌柜王于貴愁眉苦臉地迎上來华糖,“玉大人,你說我怎么就攤上這事瘟裸】筒妫” “怎么了?”我有些...
    開封第一講書人閱讀 164,133評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長兼搏。 經(jīng)常有香客問我卵慰,道長,這世上最難降的妖魔是什么佛呻? 我笑而不...
    開封第一講書人閱讀 58,532評論 1 293
  • 正文 為了忘掉前任裳朋,我火速辦了婚禮,結(jié)果婚禮上吓著,老公的妹妹穿的比我還像新娘鲤嫡。我一直安慰自己,他們只是感情好绑莺,可當我...
    茶點故事閱讀 67,585評論 6 392
  • 文/花漫 我一把揭開白布暖眼。 她就那樣靜靜地躺著,像睡著了一般纺裁。 火紅的嫁衣襯著肌膚如雪诫肠。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,462評論 1 302
  • 那天欺缘,我揣著相機與錄音栋豫,去河邊找鬼。 笑死浪南,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的漱受。 我是一名探鬼主播络凿,決...
    沈念sama閱讀 40,262評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼昂羡!你這毒婦竟也來了絮记?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,153評論 0 276
  • 序言:老撾萬榮一對情侶失蹤虐先,失蹤者是張志新(化名)和其女友劉穎怨愤,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體蛹批,經(jīng)...
    沈念sama閱讀 45,587評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡撰洗,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,792評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了腐芍。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片差导。...
    茶點故事閱讀 39,919評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖猪勇,靈堂內(nèi)的尸體忽然破棺而出设褐,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 35,635評論 5 345
  • 正文 年R本政府宣布助析,位于F島的核電站犀被,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏外冀。R本人自食惡果不足惜寡键,卻給世界環(huán)境...
    茶點故事閱讀 41,237評論 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望锥惋。 院中可真熱鬧昌腰,春花似錦、人聲如沸膀跌。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,855評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽捅伤。三九已至劫流,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間丛忆,已是汗流浹背祠汇。 一陣腳步聲響...
    開封第一講書人閱讀 32,983評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留熄诡,地道東北人可很。 一個月前我還...
    沈念sama閱讀 48,048評論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像凰浮,于是被迫代替她去往敵國和親我抠。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,864評論 2 354

推薦閱讀更多精彩內(nèi)容