在我发现了一个https://www.youtube.com/watch?v=9VcGHj36LPE&t=921s教程之后,我已经编写了没有情节提要的侧边菜单。但是,侧面菜单在您按下按钮时起作用,但是在您点击屏幕的另一部分时不会返回。我看不到哪里出了问题,可以请您帮忙吗?
这是视图控制器
class ViewController: UIViewController {
var sidebarView: SidebarView!
var blackScreen: UIView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.title = "HOME"
let btn = UIBarButtonItem(image:#imageLiteral(resourceName: "MenuButton"), style: .plain, target: self, action: #selector(btnMenuAction))
btn.tintColor = UIColor.green
self.navigationItem.leftBarButtonItem = btn
sidebarView = SidebarView(frame: CGRect(x: 0, y: 0, width: 0, height: self.view.frame.height))
sidebarView.delegate = self
sidebarView.layer.zPosition = 100
self.view.isUserInteractionEnabled = true
self.navigationController?.view.addSubview(sidebarView)
blackScreen = UIView(frame: self.view.bounds)
blackScreen.backgroundColor = UIColor(white:0, alpha:0.5)
blackScreen.isHidden = true
self.navigationController?.view.addSubview(blackScreen)
blackScreen.layer.zPosition = 99
let tapGestureRecogniser = UITapGestureRecognizer(target: self, action: #selector(blackScreenTapAction(sender:)))
blackScreen.addGestureRecognizer(tapGestureRecogniser)
}
@objc func btnMenuAction () {
blackScreen.isHidden = false
UIView.animate(withDuration: 0.3, animations: {
self.sidebarView.frame = CGRect(x:0, y:0, width:250, height: self.sidebarView.frame.height)} )
{(complete) in
self.blackScreen.frame = CGRect(x:self.sidebarView.frame.width, y:0, width: self.view.frame.width-self.sidebarView.frame.width, height: self.view.bounds.height+100)
}
}
@objc func blackScreenTapAction(sender:UITapGestureRecognizer) {
blackScreen.isHidden = true
blackScreen.frame = self.view.bounds
UIView.animate(withDuration: 0.3) {
self.sidebarView.frame = CGRect(x:0, y:0, width:250, height:self.sidebarView.frame.height)
}
}
}
extension ViewController: sideBarViewDelegate {
func sidebarDidSelectRow(row: Row) {
print("row: \(row)")
blackScreen.isHidden = true
blackScreen.frame = self.view.bounds
UIView.animate(withDuration: 0.3) {
self.sidebarView.frame = CGRect(x: 0, y: 0, width: 250, height: self.sidebarView.frame.height)
self.view.removeFromSuperview()
self.removeFromParentViewController()
}
switch row {
case.Profile:
print("Profile")
case.Home:
let vc = HelloVC()
self.navigationController?.pushViewController(vc, animated: true)
print("Home")
case .Inbox:
print("Inbox")
case.Destination:
print("Destination")
case .Payment:
print("Payment")
case .About:
print("About")
case.none:
print("none")
break
}
}
}
这是侧面菜单的代码
protocol sideBarViewDelegate: class {
func sidebarDidSelectRow(row: Row)
}
enum Row: String {
case Profile
case Home
case Inbox
case Destination
case Payment
case About
case none
init(row:Int){
switch row {
case 0: self = .Profile
case 1: self = .Home
case 2: self = .Inbox
case 3: self = .Destination
case 4: self = .Payment
case 5: self = .About
default: self = .none
}
}
}
class SidebarView: UIView, UITableViewDelegate, UITableViewDataSource {
var titleArr = [String]()
weak var delegate: sideBarViewDelegate?
override init(frame: CGRect) {
super.init(frame: frame)
self.backgroundColor = UIColor.gray
self.clipsToBounds = true
titleArr = ["Profile", "Home", "Inbox","Destination", "Payment", "About" ]
setupViews()
myTableView.delegate = self
myTableView.dataSource = self
myTableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
myTableView.tableFooterView = UIView()
myTableView.separatorStyle=UITableViewCellSeparatorStyle.none
myTableView.allowsSelection = true
myTableView.bounces = false
myTableView.showsVerticalScrollIndicator=false
myTableView.backgroundColor=UIColor.clear
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return titleArr.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
cell.backgroundColor = .clear
cell.selectionStyle = .none
if indexPath.row == 0 {
cell.backgroundColor = UIColor.darkGray
let cellImg:UIImageView!
cellImg = UIImageView(frame: CGRect(x: 15, y: 10, width: 80, height: 80))
cellImg.layer.cornerRadius = 45
cellImg.layer.masksToBounds = true
cellImg.contentMode = .scaleAspectFill
cellImg.layer.masksToBounds = true
cellImg.image = #imageLiteral(resourceName: "IMG_0051")
cell.addSubview(cellImg)
let cellLbl = UILabel(frame: CGRect(x: 110, y: cell.frame.height/2-15, width: 250, height: 40))
cell.addSubview(cellLbl)
cellLbl.text = titleArr[indexPath.row]
cellLbl.font = UIFont.systemFont(ofSize: 17)
cellLbl.textColor = UIColor.white
}else{
cell.textLabel?.text = titleArr[indexPath.row]
cell.textLabel?.textColor = UIColor.white
}
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.delegate?.sidebarDidSelectRow(row: Row(row: indexPath.row))
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.row == 0 {
return 100
} else {
return 60
}
}
func setupViews() {
self.addSubview(myTableView)
myTableView.topAnchor.constraint(equalTo:topAnchor).isActive = true
myTableView.leftAnchor.constraint(equalTo:leftAnchor).isActive = true
myTableView.rightAnchor.constraint(equalTo:rightAnchor).isActive = true
myTableView.bottomAnchor.constraint(equalTo:bottomAnchor).isActive = true
}
let myTableView:UITableView = {
let table = UITableView()
table.translatesAutoresizingMaskIntoConstraints = false
return table
} ()
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implmented")
}
}