先放预览结果图:
以及swift文件:
接下来直接贴代码加注释:MenuView.swift
import UIKit
//自定义的Delegate,用于处理menu的单击事件回调
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内容用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!)
//设置记时器每0.01秒触发一次
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()
//设立设置的三角形在中间,具体位置可以自定
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()
//设置tableView的frame,用于动画效果
tableView?.frame = CGRect(x: 0, y: 8, width: frame.size.width, height: frame.size.height-8)
}
//menu item的数量
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回调
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) {
//创建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的代理回调
func didClickSelectedRow(index: Int, title: String, menu: MenuView) {
print("\(index)----\(title)")
menu.removeMenu()
}
}